<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `hr_payroll_runs` (
            `id`                    BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `period_id`             BIGINT UNSIGNED NOT NULL,
            `employee_profile_id`   BIGINT UNSIGNED NOT NULL,
            `employee_number`       VARCHAR(20) NOT NULL,
            `department_id`         BIGINT UNSIGNED NULL,
            `job_title_id`          BIGINT UNSIGNED NULL,
            `basic_salary`          DECIMAL(15,2) NOT NULL,
            `gross_earnings`        DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `total_allowances`      DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `overtime_amount`       DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `total_deductions`      DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `insurance_employee`    DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `insurance_employer`    DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `tax_amount`            DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `loan_deduction`        DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `penalty_deduction`     DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `absence_deduction`     DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `other_deductions`      DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `other_earnings`        DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `net_salary`            DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `working_days`          INT UNSIGNED NOT NULL DEFAULT 0,
            `present_days`          INT UNSIGNED NOT NULL DEFAULT 0,
            `absent_days`           INT UNSIGNED NOT NULL DEFAULT 0,
            `leave_days`            DECIMAL(5,1) NOT NULL DEFAULT 0.0,
            `overtime_hours`        DECIMAL(6,2) NOT NULL DEFAULT 0.00,
            `calculation_json`      JSON NULL,
            `status`                VARCHAR(20) NOT NULL DEFAULT 'calculated' COMMENT 'calculated, approved, paid, cancelled',
            `payment_method`        VARCHAR(20) NULL COMMENT 'bank_transfer, cash, check',
            `payment_reference`     VARCHAR(100) NULL,
            `paid_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,
            `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_payroll_run` (`period_id`, `employee_profile_id`),
            INDEX `idx_hr_payroll_run_period` (`period_id`),
            INDEX `idx_hr_payroll_run_employee` (`employee_profile_id`),
            INDEX `idx_hr_payroll_run_status` (`status`),
            CONSTRAINT `fk_hr_payroll_run_period` FOREIGN KEY (`period_id`) REFERENCES `hr_payroll_periods`(`id`),
            CONSTRAINT `chk_hr_payroll_run_status` CHECK (`status` IN ('calculated', 'approved', 'paid', 'cancelled')),
            CONSTRAINT `chk_hr_payroll_run_gross` CHECK (`gross_earnings` >= 0),
            CONSTRAINT `chk_hr_payroll_run_net` CHECK (`net_salary` >= 0)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `hr_payroll_runs`",
];
