<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `hr_employee_loans` (
            `id`                    BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `employee_profile_id`   BIGINT UNSIGNED NOT NULL,
            `loan_number`           VARCHAR(30) NOT NULL,
            `loan_type`             VARCHAR(30) NOT NULL COMMENT 'salary_advance, personal_loan, emergency_loan',
            `loan_amount`           DECIMAL(15,2) NOT NULL,
            `installment_amount`    DECIMAL(15,2) NOT NULL,
            `number_of_installments` INT UNSIGNED NOT NULL,
            `paid_installments`     INT UNSIGNED NOT NULL DEFAULT 0,
            `remaining_amount`      DECIMAL(15,2) NOT NULL,
            `interest_rate`         DECIMAL(5,2) NOT NULL DEFAULT 0.00,
            `request_date`          DATE NULL,
            `start_deduction_date`  DATE NOT NULL,
            `reason`                TEXT NULL,
            `status`                VARCHAR(20) NOT NULL DEFAULT 'pending' COMMENT 'pending, approved, disbursed, repaying, settled, rejected, cancelled',
            `approved_by`           BIGINT UNSIGNED NULL,
            `approved_at`           TIMESTAMP NULL DEFAULT NULL,
            `disbursed_date`        DATE NULL,
            `workflow_instance_id`  BIGINT UNSIGNED NULL,
            `notes`                 TEXT NULL,
            `created_at`            TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at`            TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            `created_by`            BIGINT UNSIGNED NULL,
            `updated_by`            BIGINT UNSIGNED NULL,
            `is_archived`           TINYINT(1) NOT NULL DEFAULT 0,
            `archived_at`           TIMESTAMP NULL DEFAULT NULL,
            `archived_by`           BIGINT UNSIGNED NULL,
            UNIQUE KEY `uq_hr_loans_number` (`loan_number`),
            INDEX `idx_hr_loans_employee` (`employee_profile_id`),
            INDEX `idx_hr_loans_status` (`status`),
            INDEX `idx_hr_loans_archived` (`is_archived`),
            CONSTRAINT `fk_hr_loans_employee` FOREIGN KEY (`employee_profile_id`) REFERENCES `hr_employee_profiles`(`id`),
            CONSTRAINT `chk_hr_loans_type` CHECK (`loan_type` IN ('salary_advance', 'personal_loan', 'emergency_loan')),
            CONSTRAINT `chk_hr_loans_status` CHECK (`status` IN ('pending', 'approved', 'disbursed', 'repaying', 'settled', 'rejected', 'cancelled')),
            CONSTRAINT `chk_hr_loans_amount` CHECK (`loan_amount` > 0)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `hr_employee_loans`",
];
