-- KINET MikroTik Admin Module - Update v13 Menu, Search, Bank Account Popup
-- Jalankan sekali setelah replace file v13.

CREATE TABLE IF NOT EXISTS bank_accounts (
    id INT AUTO_INCREMENT PRIMARY KEY,
    label VARCHAR(120) NOT NULL,
    type ENUM('bank','ewallet') NOT NULL DEFAULT 'bank',
    bank_name VARCHAR(120) NOT NULL,
    account_number VARCHAR(120) NOT NULL,
    account_name VARCHAR(160) NOT NULL,
    code VARCHAR(80) NOT NULL,
    note TEXT NULL,
    is_default TINYINT(1) NOT NULL DEFAULT 0,
    status ENUM('active','inactive') NOT NULL DEFAULT 'active',
    sort_order INT NOT NULL DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uq_bank_code (code),
    INDEX idx_bank_status (status),
    INDEX idx_bank_default (is_default)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO bank_accounts (label, type, bank_name, account_number, account_name, code, note, is_default, status, sort_order)
SELECT 'Default Manual', 'bank', 'BANK', '0000000000', 'KINET', 'default', 'Ganti rekening ini dari menu Rekening Bank.', 1, 'active', 0
WHERE NOT EXISTS (SELECT 1 FROM bank_accounts);

INSERT INTO settings (setting_key, setting_value) VALUES
('template_bank_accounts_enabled','1')
ON DUPLICATE KEY UPDATE setting_value = setting_value;
