<?php
declare(strict_types=1);

return [
    'up' => "
CREATE TABLE IF NOT EXISTS bank_loans (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    loan_number VARCHAR(50) NOT NULL,
    bank_account_id INT UNSIGNED NOT NULL,
    loan_type ENUM('term','revolving','overdraft','mortgage') NOT NULL DEFAULT 'term',
    principal_amount DECIMAL(15,2) NOT NULL,
    interest_rate DECIMAL(5,4) NOT NULL,
    term_months INT UNSIGNED NOT NULL,
    start_date DATE NOT NULL,
    end_date DATE NOT NULL,
    monthly_payment DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    outstanding_balance DECIMAL(15,2) NOT NULL,
    total_interest DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    total_paid DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    status ENUM('active','paid_off','defaulted','restructured') NOT NULL DEFAULT 'active',
    collateral_description TEXT NULL,
    notes TEXT NULL,
    journal_entry_id INT UNSIGNED NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NULL ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_loan_number (loan_number),
    KEY idx_bank (bank_account_id),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS bank_loan_schedule (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    loan_id INT UNSIGNED NOT NULL,
    installment_number INT UNSIGNED NOT NULL,
    due_date DATE NOT NULL,
    principal_amount DECIMAL(15,2) NOT NULL,
    interest_amount DECIMAL(15,2) NOT NULL,
    total_amount DECIMAL(15,2) NOT NULL,
    paid_amount DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    paid_date DATE NULL,
    status ENUM('upcoming','due','paid','overdue') NOT NULL DEFAULT 'upcoming',
    payment_reference VARCHAR(100) NULL,
    journal_entry_id INT UNSIGNED NULL,
    KEY idx_loan (loan_id),
    KEY idx_due_date (due_date),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS daily_cash_movements (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    movement_date DATE NOT NULL,
    bank_account_id INT UNSIGNED NULL,
    safe_id INT UNSIGNED NULL,
    opening_balance DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    total_inflows DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    total_outflows DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    closing_balance DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    is_reconciled TINYINT(1) NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uk_date_bank (movement_date, bank_account_id),
    UNIQUE KEY uk_date_safe (movement_date, safe_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
    'down' => "
DROP TABLE IF EXISTS daily_cash_movements;
DROP TABLE IF EXISTS bank_loan_schedule;
DROP TABLE IF EXISTS bank_loans",
];
