<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `hr_loan_installments` (
            `id`                    BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `loan_id`               BIGINT UNSIGNED NOT NULL,
            `installment_number`    INT UNSIGNED NOT NULL,
            `due_date`              DATE NOT NULL,
            `amount`                DECIMAL(15,2) NOT NULL,
            `status`                VARCHAR(20) NOT NULL DEFAULT 'pending' COMMENT 'pending, deducted, skipped, cancelled',
            `payroll_run_id`        BIGINT UNSIGNED NULL,
            `deducted_at`           TIMESTAMP NULL DEFAULT NULL,
            `notes`                 TEXT NULL,
            `created_at`            TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at`            TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            INDEX `idx_hr_loan_inst_loan` (`loan_id`),
            INDEX `idx_hr_loan_inst_month` (`due_date`),
            INDEX `idx_hr_loan_inst_status` (`status`),
            CONSTRAINT `fk_hr_loan_inst_loan` FOREIGN KEY (`loan_id`) REFERENCES `hr_employee_loans`(`id`) ON DELETE CASCADE,
            CONSTRAINT `chk_hr_loan_inst_status` CHECK (`status` IN ('pending', 'deducted', 'skipped', 'cancelled')),
            CONSTRAINT `chk_hr_loan_inst_amount` CHECK (`amount` >= 0)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `hr_loan_installments`",
];
