<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `hr_salary_components` (
            `id`                    BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `structure_id`          BIGINT UNSIGNED NOT NULL,
            `component_code`        VARCHAR(30) NOT NULL,
            `name_ar`               VARCHAR(200) NOT NULL,
            `name_en`               VARCHAR(200) NULL,
            `type`                  VARCHAR(20) NOT NULL COMMENT 'earning or deduction',
            `calculation_type`      VARCHAR(30) NOT NULL COMMENT 'fixed, percentage_of_basic, percentage_of_gross, formula',
            `default_amount`        DECIMAL(15,2) NULL,
            `default_percentage`    DECIMAL(5,2) NULL,
            `percentage_of`         VARCHAR(30) NULL COMMENT 'basic, gross, etc.',
            `percentage_value`      DECIMAL(5,2) NULL,
            `formula_json`          JSON NULL,
            `is_taxable`            TINYINT(1) NOT NULL DEFAULT 1,
            `is_insurable`          TINYINT(1) NOT NULL DEFAULT 1,
            `is_basic`              TINYINT(1) NOT NULL DEFAULT 0,
            `is_variable`           TINYINT(1) NOT NULL DEFAULT 0,
            `is_system_calculated`  TINYINT(1) NOT NULL DEFAULT 0,
            `is_mandatory`          TINYINT(1) NOT NULL DEFAULT 0,
            `affects_insurance_base` TINYINT(1) NOT NULL DEFAULT 0,
            `affects_overtime`      TINYINT(1) NOT NULL DEFAULT 0,
            `sort_order`            INT UNSIGNED NOT NULL DEFAULT 0,
            `is_active`             TINYINT(1) NOT NULL DEFAULT 1,
            `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,
            INDEX `idx_hr_salary_comp_structure` (`structure_id`),
            UNIQUE KEY `uq_hr_salary_comp_code` (`structure_id`, `component_code`),
            INDEX `idx_hr_salary_comp_type` (`type`),
            CONSTRAINT `fk_hr_salary_comp_structure` FOREIGN KEY (`structure_id`) REFERENCES `hr_salary_structures`(`id`) ON DELETE CASCADE,
            CONSTRAINT `chk_hr_salary_comp_type` CHECK (`type` IN ('earning', 'deduction')),
            CONSTRAINT `chk_hr_salary_calc_type` CHECK (`calculation_type` IN ('fixed', 'percentage_of_basic', 'percentage_of_gross', 'formula'))
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `hr_salary_components`",
];
