<?php
declare(strict_types=1);

use App\Core\Database;

/**
 * Default Salary Structure with 14 Components
 *
 * Earnings (8):
 *   1. Basic Salary (base for all calculations)
 *   2. Housing Allowance (percentage of basic)
 *   3. Transportation Allowance (fixed amount)
 *   4. Meal Allowance (fixed amount)
 *   5. Social Allowance (percentage of basic, for married/children)
 *   6. Nature of Work Allowance (percentage of basic, hazardous/special)
 *   7. Overtime Pay (calculated by service, not manually set)
 *   8. Other Allowances (custom/one-off)
 *
 * Deductions (6):
 *   9.  Social Insurance Deduction (calculated by service)
 *   10. Income Tax Deduction (calculated by service)
 *   11. Loan Installment Deduction (calculated by service)
 *   12. Disciplinary Penalty Deduction (calculated by service)
 *   13. Absence Deduction (calculated by service)
 *   14. Syndicate/Union Fee (fixed amount)
 */
return function (Database $db): void {
    $ts = date('Y-m-d H:i:s');

    // ── Create default salary structure ──
    $existingStructure = $db->selectOne(
        "SELECT id FROM hr_salary_structures WHERE code = ?",
        ['DEFAULT']
    );

    if ($existingStructure) {
        $structureId = (int) $existingStructure['id'];
    } else {
        $structureId = $db->insert('hr_salary_structures', [
            'code'        => 'DEFAULT',
            'name_ar'     => 'الهيكل الراتبي الأساسي',
            'name_en'     => 'Default Salary Structure',
            'description' => 'الهيكل الراتبي القياسي للنادي - يشمل جميع بنود الاستحقاقات والاستقطاعات الأساسية',
            'is_default'  => 1,
            'is_active'   => 1,
            'created_at'  => $ts,
            'updated_at'  => $ts,
        ]);
    }

    // ── Define the 14 components ──
    $components = [
        // ── Earnings ──
        [
            'component_code'    => 'BASIC',
            'name_ar'           => 'الراتب الأساسي',
            'name_en'           => 'Basic Salary',
            'type'              => 'earning',
            'calculation_type'  => 'fixed',
            'default_amount'    => '0.00',
            'percentage_of'     => null,
            'percentage_value'  => null,
            'is_taxable'        => 1,
            'is_insurable'      => 1,
            'is_basic'          => 1,
            'is_variable'       => 0,
            'is_system_calculated' => 0,
            'is_mandatory'      => 1,
            'affects_overtime'  => 1,
            'sort_order'        => 1,
        ],
        [
            'component_code'    => 'HOUSING',
            'name_ar'           => 'بدل سكن',
            'name_en'           => 'Housing Allowance',
            'type'              => 'earning',
            'calculation_type'  => 'percentage_of_basic',
            'default_amount'    => '0.00',
            'percentage_of'     => 'basic',
            'percentage_value'  => '25.00',
            'is_taxable'        => 1,
            'is_insurable'      => 0,
            'is_basic'          => 0,
            'is_variable'       => 1,
            'is_system_calculated' => 0,
            'is_mandatory'      => 0,
            'affects_overtime'  => 0,
            'sort_order'        => 2,
        ],
        [
            'component_code'    => 'TRANSPORT',
            'name_ar'           => 'بدل انتقال',
            'name_en'           => 'Transportation Allowance',
            'type'              => 'earning',
            'calculation_type'  => 'fixed',
            'default_amount'    => '500.00',
            'percentage_of'     => null,
            'percentage_value'  => null,
            'is_taxable'        => 1,
            'is_insurable'      => 0,
            'is_basic'          => 0,
            'is_variable'       => 1,
            'is_system_calculated' => 0,
            'is_mandatory'      => 0,
            'affects_overtime'  => 0,
            'sort_order'        => 3,
        ],
        [
            'component_code'    => 'MEAL',
            'name_ar'           => 'بدل وجبات',
            'name_en'           => 'Meal Allowance',
            'type'              => 'earning',
            'calculation_type'  => 'fixed',
            'default_amount'    => '300.00',
            'percentage_of'     => null,
            'percentage_value'  => null,
            'is_taxable'        => 1,
            'is_insurable'      => 0,
            'is_basic'          => 0,
            'is_variable'       => 1,
            'is_system_calculated' => 0,
            'is_mandatory'      => 0,
            'affects_overtime'  => 0,
            'sort_order'        => 4,
        ],
        [
            'component_code'    => 'SOCIAL',
            'name_ar'           => 'علاوة اجتماعية',
            'name_en'           => 'Social Allowance',
            'type'              => 'earning',
            'calculation_type'  => 'percentage_of_basic',
            'default_amount'    => '0.00',
            'percentage_of'     => 'basic',
            'percentage_value'  => '10.00',
            'is_taxable'        => 1,
            'is_insurable'      => 0,
            'is_basic'          => 0,
            'is_variable'       => 1,
            'is_system_calculated' => 0,
            'is_mandatory'      => 0,
            'affects_overtime'  => 0,
            'sort_order'        => 5,
        ],
        [
            'component_code'    => 'NATURE_WORK',
            'name_ar'           => 'بدل طبيعة عمل',
            'name_en'           => 'Nature of Work Allowance',
            'type'              => 'earning',
            'calculation_type'  => 'percentage_of_basic',
            'default_amount'    => '0.00',
            'percentage_of'     => 'basic',
            'percentage_value'  => '0.00',
            'is_taxable'        => 1,
            'is_insurable'      => 0,
            'is_basic'          => 0,
            'is_variable'       => 1,
            'is_system_calculated' => 0,
            'is_mandatory'      => 0,
            'affects_overtime'  => 0,
            'sort_order'        => 6,
        ],
        [
            'component_code'    => 'OVERTIME',
            'name_ar'           => 'أجر إضافي',
            'name_en'           => 'Overtime Pay',
            'type'              => 'earning',
            'calculation_type'  => 'formula',
            'default_amount'    => '0.00',
            'percentage_of'     => null,
            'percentage_value'  => null,
            'is_taxable'        => 1,
            'is_insurable'      => 0,
            'is_basic'          => 0,
            'is_variable'       => 1,
            'is_system_calculated' => 1,
            'is_mandatory'      => 1,
            'affects_overtime'  => 0,
            'sort_order'        => 7,
        ],
        [
            'component_code'    => 'OTHER_EARNING',
            'name_ar'           => 'بدلات أخرى',
            'name_en'           => 'Other Allowances',
            'type'              => 'earning',
            'calculation_type'  => 'fixed',
            'default_amount'    => '0.00',
            'percentage_of'     => null,
            'percentage_value'  => null,
            'is_taxable'        => 1,
            'is_insurable'      => 0,
            'is_basic'          => 0,
            'is_variable'       => 1,
            'is_system_calculated' => 0,
            'is_mandatory'      => 0,
            'affects_overtime'  => 0,
            'sort_order'        => 8,
        ],

        // ── Deductions ──
        [
            'component_code'    => 'INSURANCE_DED',
            'name_ar'           => 'حصة الموظف في التأمينات',
            'name_en'           => 'Social Insurance Deduction',
            'type'              => 'deduction',
            'calculation_type'  => 'formula',
            'default_amount'    => '0.00',
            'percentage_of'     => null,
            'percentage_value'  => null,
            'is_taxable'        => 0,
            'is_insurable'      => 0,
            'is_basic'          => 0,
            'is_variable'       => 0,
            'is_system_calculated' => 1,
            'is_mandatory'      => 1,
            'affects_overtime'  => 0,
            'sort_order'        => 9,
        ],
        [
            'component_code'    => 'TAX_DED',
            'name_ar'           => 'ضريبة كسب العمل',
            'name_en'           => 'Income Tax Deduction',
            'type'              => 'deduction',
            'calculation_type'  => 'formula',
            'default_amount'    => '0.00',
            'percentage_of'     => null,
            'percentage_value'  => null,
            'is_taxable'        => 0,
            'is_insurable'      => 0,
            'is_basic'          => 0,
            'is_variable'       => 0,
            'is_system_calculated' => 1,
            'is_mandatory'      => 1,
            'affects_overtime'  => 0,
            'sort_order'        => 10,
        ],
        [
            'component_code'    => 'LOAN_DED',
            'name_ar'           => 'قسط سلفة / قرض',
            'name_en'           => 'Loan Installment Deduction',
            'type'              => 'deduction',
            'calculation_type'  => 'formula',
            'default_amount'    => '0.00',
            'percentage_of'     => null,
            'percentage_value'  => null,
            'is_taxable'        => 0,
            'is_insurable'      => 0,
            'is_basic'          => 0,
            'is_variable'       => 0,
            'is_system_calculated' => 1,
            'is_mandatory'      => 1,
            'affects_overtime'  => 0,
            'sort_order'        => 11,
        ],
        [
            'component_code'    => 'PENALTY_DED',
            'name_ar'           => 'خصم جزاء تأديبي',
            'name_en'           => 'Disciplinary Penalty Deduction',
            'type'              => 'deduction',
            'calculation_type'  => 'formula',
            'default_amount'    => '0.00',
            'percentage_of'     => null,
            'percentage_value'  => null,
            'is_taxable'        => 0,
            'is_insurable'      => 0,
            'is_basic'          => 0,
            'is_variable'       => 0,
            'is_system_calculated' => 1,
            'is_mandatory'      => 1,
            'affects_overtime'  => 0,
            'sort_order'        => 12,
        ],
        [
            'component_code'    => 'ABSENCE_DED',
            'name_ar'           => 'خصم غياب',
            'name_en'           => 'Absence Deduction',
            'type'              => 'deduction',
            'calculation_type'  => 'formula',
            'default_amount'    => '0.00',
            'percentage_of'     => null,
            'percentage_value'  => null,
            'is_taxable'        => 0,
            'is_insurable'      => 0,
            'is_basic'          => 0,
            'is_variable'       => 0,
            'is_system_calculated' => 1,
            'is_mandatory'      => 1,
            'affects_overtime'  => 0,
            'sort_order'        => 13,
        ],
        [
            'component_code'    => 'SYNDICATE_DED',
            'name_ar'           => 'اشتراك نقابة',
            'name_en'           => 'Syndicate/Union Fee',
            'type'              => 'deduction',
            'calculation_type'  => 'fixed',
            'default_amount'    => '0.00',
            'percentage_of'     => null,
            'percentage_value'  => null,
            'is_taxable'        => 0,
            'is_insurable'      => 0,
            'is_basic'          => 0,
            'is_variable'       => 0,
            'is_system_calculated' => 0,
            'is_mandatory'      => 0,
            'affects_overtime'  => 0,
            'sort_order'        => 14,
        ],
    ];

    foreach ($components as $comp) {
        $existing = $db->selectOne(
            "SELECT id FROM hr_salary_components WHERE structure_id = ? AND component_code = ?",
            [$structureId, $comp['component_code']]
        );
        if ($existing) {
            continue;
        }

        $db->insert('hr_salary_components', [
            'structure_id'         => $structureId,
            'component_code'       => $comp['component_code'],
            'name_ar'              => $comp['name_ar'],
            'name_en'              => $comp['name_en'],
            'type'                 => $comp['type'],
            'calculation_type'     => $comp['calculation_type'],
            'default_amount'       => $comp['default_amount'],
            'percentage_of'        => $comp['percentage_of'],
            'percentage_value'     => $comp['percentage_value'],
            'is_taxable'           => $comp['is_taxable'],
            'is_insurable'         => $comp['is_insurable'],
            'is_basic'             => $comp['is_basic'],
            'is_variable'          => $comp['is_variable'],
            'is_system_calculated' => $comp['is_system_calculated'],
            'is_mandatory'         => $comp['is_mandatory'],
            'affects_overtime'     => $comp['affects_overtime'],
            'sort_order'           => $comp['sort_order'],
            'is_active'            => 1,
            'created_at'           => $ts,
            'updated_at'           => $ts,
        ]);
    }
};
