<?php
declare(strict_types=1);

use App\Core\Database;

/**
 * Egyptian Labour Law Leave Types (Law 12/2003, Articles 47-54, 91)
 *
 * Annual Leave:
 *   Art.47: 21 days/year (service < 10y), 30 days/year (service >= 10y OR age >= 50)
 *   Art.48: 45 days/year for hazardous/remote work
 *   Art.47: 15 days/year for workers under 18
 *   Art.48: Minimum 6 consecutive days per year
 *   Art.48: Max carry-over 3 years accumulation
 *
 * Sick Leave:
 *   Art.54: Employee entitled to sick leave certified by medical authority
 *   First 3 months: 75% pay, Next 3 months: 85% pay, then unpaid
 *   Medical committee required after initial period
 *
 * Maternity:
 *   Art.91: 90 days paid leave, max 3 times during career
 *   Cannot terminate during leave or within 1 year after return
 *
 * Casual/Emergency:
 *   Art.51: 7 days/year, max 2 consecutive days per request
 *
 * Hajj/Pilgrimage:
 *   Art.53: 30 days unpaid, once in entire career
 *
 * Study:
 *   Art.52: As per employment contract or company policy
 *
 * Marriage: 3 days paid
 * Bereavement: 5 days paid (death of spouse or first/second degree relative)
 * Nursing: 1 hour/day reduction for 2 years after childbirth (Art.93)
 */
return function (Database $db): void {
    $ts = date('Y-m-d H:i:s');

    $leaveTypes = [
        [
            'code'                    => 'annual',
            'name_ar'                 => 'إجازة سنوية',
            'name_en'                 => 'Annual Leave',
            'category'                => 'annual',
            'default_days_per_year'   => '21.0',
            'max_days_per_year'       => '45.0',
            'pay_percentage'          => '100.00',
            'is_paid'                 => 1,
            'requires_approval'       => 1,
            'requires_attachment'     => 0,
            'min_days_per_request'    => '0.5',
            'max_days_per_request'    => '30.0',
            'min_consecutive_annual'  => '6.0',
            'advance_notice_days'     => 7,
            'carry_over_allowed'      => 1,
            'max_carry_over_days'     => '90.0',
            'carry_over_expiry_months'=> 36,
            'accumulation_max_years'  => 3,
            'gender_restriction'      => null,
            'career_max_times'        => null,
            'career_max_days'         => null,
            'legal_reference'         => 'Art. 47-50',
            'is_active'               => 1,
            'sort_order'              => 1,
            'rules_json'              => json_encode([
                'service_less_10y'     => 21,
                'service_gte_10y'      => 30,
                'age_gte_50'           => 30,
                'hazardous_work'       => 45,
                'under_18'             => 15,
                'min_consecutive_days' => 6,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code'                    => 'sick',
            'name_ar'                 => 'إجازة مرضية',
            'name_en'                 => 'Sick Leave',
            'category'                => 'sick',
            'default_days_per_year'   => '180.0',
            'max_days_per_year'       => '180.0',
            'pay_percentage'          => '75.00',
            'is_paid'                 => 1,
            'requires_approval'       => 1,
            'requires_attachment'     => 1,
            'min_days_per_request'    => '1.0',
            'max_days_per_request'    => '90.0',
            'min_consecutive_annual'  => null,
            'advance_notice_days'     => 0,
            'carry_over_allowed'      => 0,
            'max_carry_over_days'     => null,
            'carry_over_expiry_months'=> null,
            'accumulation_max_years'  => null,
            'gender_restriction'      => null,
            'career_max_times'        => null,
            'career_max_days'         => null,
            'legal_reference'         => 'Art. 54',
            'is_active'               => 1,
            'sort_order'              => 2,
            'rules_json'              => json_encode([
                'first_3_months_pct'  => 75,
                'next_3_months_pct'   => 85,
                'after_6_months_pct'  => 0,
                'requires_medical_committee_after_days' => 90,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code'                    => 'maternity',
            'name_ar'                 => 'إجازة وضع (أمومة)',
            'name_en'                 => 'Maternity Leave',
            'category'                => 'special',
            'default_days_per_year'   => '90.0',
            'max_days_per_year'       => '90.0',
            'pay_percentage'          => '100.00',
            'is_paid'                 => 1,
            'requires_approval'       => 1,
            'requires_attachment'     => 1,
            'min_days_per_request'    => '90.0',
            'max_days_per_request'    => '90.0',
            'min_consecutive_annual'  => null,
            'advance_notice_days'     => 0,
            'carry_over_allowed'      => 0,
            'max_carry_over_days'     => null,
            'carry_over_expiry_months'=> null,
            'accumulation_max_years'  => null,
            'gender_restriction'      => 'female',
            'career_max_times'        => 3,
            'career_max_days'         => '270.0',
            'legal_reference'         => 'Art. 91',
            'is_active'               => 1,
            'sort_order'              => 3,
            'rules_json'              => json_encode([
                'max_times_career'               => 3,
                'termination_protection_months'  => 12,
                'min_service_months'             => 10,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code'                    => 'casual',
            'name_ar'                 => 'إجازة عارضة',
            'name_en'                 => 'Casual Leave',
            'category'                => 'casual',
            'default_days_per_year'   => '7.0',
            'max_days_per_year'       => '7.0',
            'pay_percentage'          => '100.00',
            'is_paid'                 => 1,
            'requires_approval'       => 1,
            'requires_attachment'     => 0,
            'min_days_per_request'    => '0.5',
            'max_days_per_request'    => '2.0',
            'min_consecutive_annual'  => null,
            'advance_notice_days'     => 0,
            'carry_over_allowed'      => 0,
            'max_carry_over_days'     => null,
            'carry_over_expiry_months'=> null,
            'accumulation_max_years'  => null,
            'gender_restriction'      => null,
            'career_max_times'        => null,
            'career_max_days'         => null,
            'legal_reference'         => 'Art. 51',
            'is_active'               => 1,
            'sort_order'              => 4,
            'rules_json'              => json_encode([
                'max_consecutive_days' => 2,
                'deducted_from_annual' => true,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code'                    => 'hajj',
            'name_ar'                 => 'إجازة حج',
            'name_en'                 => 'Hajj Leave',
            'category'                => 'special',
            'default_days_per_year'   => '30.0',
            'max_days_per_year'       => '30.0',
            'pay_percentage'          => '0.00',
            'is_paid'                 => 0,
            'requires_approval'       => 1,
            'requires_attachment'     => 1,
            'min_days_per_request'    => '30.0',
            'max_days_per_request'    => '30.0',
            'min_consecutive_annual'  => null,
            'advance_notice_days'     => 30,
            'carry_over_allowed'      => 0,
            'max_carry_over_days'     => null,
            'carry_over_expiry_months'=> null,
            'accumulation_max_years'  => null,
            'gender_restriction'      => null,
            'career_max_times'        => 1,
            'career_max_days'         => '30.0',
            'legal_reference'         => 'Art. 53',
            'is_active'               => 1,
            'sort_order'              => 5,
            'rules_json'              => json_encode([
                'once_in_career'       => true,
                'min_service_years'    => 5,
                'religion_restriction' => 'muslim',
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code'                    => 'marriage',
            'name_ar'                 => 'إجازة زواج',
            'name_en'                 => 'Marriage Leave',
            'category'                => 'special',
            'default_days_per_year'   => '3.0',
            'max_days_per_year'       => '3.0',
            'pay_percentage'          => '100.00',
            'is_paid'                 => 1,
            'requires_approval'       => 1,
            'requires_attachment'     => 1,
            'min_days_per_request'    => '3.0',
            'max_days_per_request'    => '3.0',
            'min_consecutive_annual'  => null,
            'advance_notice_days'     => 7,
            'carry_over_allowed'      => 0,
            'max_carry_over_days'     => null,
            'carry_over_expiry_months'=> null,
            'accumulation_max_years'  => null,
            'gender_restriction'      => null,
            'career_max_times'        => null,
            'career_max_days'         => null,
            'legal_reference'         => 'Company Policy / Custom',
            'is_active'               => 1,
            'sort_order'              => 6,
            'rules_json'              => json_encode([
                'requires_marriage_certificate' => true,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code'                    => 'bereavement',
            'name_ar'                 => 'إجازة وفاة',
            'name_en'                 => 'Bereavement Leave',
            'category'                => 'special',
            'default_days_per_year'   => '5.0',
            'max_days_per_year'       => '15.0',
            'pay_percentage'          => '100.00',
            'is_paid'                 => 1,
            'requires_approval'       => 1,
            'requires_attachment'     => 1,
            'min_days_per_request'    => '3.0',
            'max_days_per_request'    => '5.0',
            'min_consecutive_annual'  => null,
            'advance_notice_days'     => 0,
            'carry_over_allowed'      => 0,
            'max_carry_over_days'     => null,
            'carry_over_expiry_months'=> null,
            'accumulation_max_years'  => null,
            'gender_restriction'      => null,
            'career_max_times'        => null,
            'career_max_days'         => null,
            'legal_reference'         => 'Company Policy / Custom',
            'is_active'               => 1,
            'sort_order'              => 7,
            'rules_json'              => json_encode([
                'spouse_days'               => 5,
                'first_degree_days'         => 5,
                'second_degree_days'        => 3,
                'requires_death_certificate'=> true,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code'                    => 'study',
            'name_ar'                 => 'إجازة دراسية',
            'name_en'                 => 'Study Leave',
            'category'                => 'special',
            'default_days_per_year'   => '0.0',
            'max_days_per_year'       => '21.0',
            'pay_percentage'          => '100.00',
            'is_paid'                 => 1,
            'requires_approval'       => 1,
            'requires_attachment'     => 1,
            'min_days_per_request'    => '1.0',
            'max_days_per_request'    => '21.0',
            'min_consecutive_annual'  => null,
            'advance_notice_days'     => 14,
            'carry_over_allowed'      => 0,
            'max_carry_over_days'     => null,
            'carry_over_expiry_months'=> null,
            'accumulation_max_years'  => null,
            'gender_restriction'      => null,
            'career_max_times'        => null,
            'career_max_days'         => null,
            'legal_reference'         => 'Art. 52',
            'is_active'               => 1,
            'sort_order'              => 8,
            'rules_json'              => json_encode([
                'requires_enrollment_proof'       => true,
                'deducted_from_annual_if_unpaid'  => false,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code'                    => 'unpaid',
            'name_ar'                 => 'إجازة بدون مرتب',
            'name_en'                 => 'Unpaid Leave',
            'category'                => 'unpaid',
            'default_days_per_year'   => '0.0',
            'max_days_per_year'       => '365.0',
            'pay_percentage'          => '0.00',
            'is_paid'                 => 0,
            'requires_approval'       => 1,
            'requires_attachment'     => 0,
            'min_days_per_request'    => '1.0',
            'max_days_per_request'    => '90.0',
            'min_consecutive_annual'  => null,
            'advance_notice_days'     => 14,
            'carry_over_allowed'      => 0,
            'max_carry_over_days'     => null,
            'carry_over_expiry_months'=> null,
            'accumulation_max_years'  => null,
            'gender_restriction'      => null,
            'career_max_times'        => null,
            'career_max_days'         => null,
            'legal_reference'         => 'Art. 51',
            'is_active'               => 1,
            'sort_order'              => 9,
            'rules_json'              => json_encode([
                'affects_service_years' => true,
                'suspends_insurance'    => true,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code'                    => 'nursing',
            'name_ar'                 => 'ساعة رضاعة',
            'name_en'                 => 'Nursing Hour',
            'category'                => 'special',
            'default_days_per_year'   => '0.0',
            'max_days_per_year'       => '0.0',
            'pay_percentage'          => '100.00',
            'is_paid'                 => 1,
            'requires_approval'       => 1,
            'requires_attachment'     => 1,
            'min_days_per_request'    => '0.0',
            'max_days_per_request'    => '0.0',
            'min_consecutive_annual'  => null,
            'advance_notice_days'     => 0,
            'carry_over_allowed'      => 0,
            'max_carry_over_days'     => null,
            'carry_over_expiry_months'=> null,
            'accumulation_max_years'  => null,
            'gender_restriction'      => 'female',
            'career_max_times'        => null,
            'career_max_days'         => null,
            'legal_reference'         => 'Art. 93',
            'is_active'               => 1,
            'sort_order'              => 10,
            'rules_json'              => json_encode([
                'hours_per_day'          => 1,
                'duration_years'         => 2,
                'can_split_into_two'     => true,
                'requires_birth_certificate' => true,
            ], JSON_UNESCAPED_UNICODE),
        ],
    ];

    foreach ($leaveTypes as $lt) {
        $existing = $db->selectOne(
            "SELECT id FROM hr_leave_types WHERE code = ?",
            [$lt['code']]
        );
        if ($existing) {
            continue;
        }

        $db->insert('hr_leave_types', [
            'code'                     => $lt['code'],
            'name_ar'                  => $lt['name_ar'],
            'name_en'                  => $lt['name_en'],
            'category'                 => $lt['category'],
            'default_days_per_year'    => $lt['default_days_per_year'],
            'max_days_per_year'        => $lt['max_days_per_year'],
            'pay_percentage'           => $lt['pay_percentage'],
            'is_paid'                  => $lt['is_paid'],
            'requires_approval'        => $lt['requires_approval'],
            'requires_attachment'      => $lt['requires_attachment'],
            'min_days_per_request'     => $lt['min_days_per_request'],
            'max_days_per_request'     => $lt['max_days_per_request'],
            'min_consecutive_annual'   => $lt['min_consecutive_annual'],
            'advance_notice_days'      => $lt['advance_notice_days'],
            'carry_over_allowed'       => $lt['carry_over_allowed'],
            'max_carry_over_days'      => $lt['max_carry_over_days'],
            'carry_over_expiry_months' => $lt['carry_over_expiry_months'],
            'accumulation_max_years'   => $lt['accumulation_max_years'],
            'gender_restriction'       => $lt['gender_restriction'],
            'career_max_times'         => $lt['career_max_times'],
            'career_max_days'          => $lt['career_max_days'],
            'legal_reference'          => $lt['legal_reference'],
            'is_active'                => $lt['is_active'],
            'sort_order'               => $lt['sort_order'],
            'rules_json'               => $lt['rules_json'],
            'created_at'               => $ts,
            'updated_at'               => $ts,
        ]);
    }
};
