<?php
declare(strict_types=1);

use App\Core\Database;

/**
 * Egyptian National and Religious Holidays
 *
 * National holidays are fixed-date (Gregorian).
 * Islamic holidays are approximate and should be updated yearly
 * based on official announcements (Hijri calendar varies).
 *
 * All employees are entitled to national holidays.
 * Coptic holidays apply to Christian employees.
 * Islamic holidays apply to Muslim employees (and often all).
 */
return function (Database $db): void {
    $ts = date('Y-m-d H:i:s');
    $currentYear = (int) date('Y');

    $holidays = [
        // ── National Holidays (Fixed Date) ──
        [
            'name_ar'           => 'عيد رأس السنة الميلادية',
            'name_en'           => 'New Year\'s Day',
            'holiday_type'      => 'national',
            'date'              => $currentYear . '-01-01',
            'is_recurring'      => 1,
            'recurring_month'   => 1,
            'recurring_day'     => 1,
            'religion'          => null,
            'is_active'         => 1,
        ],
        [
            'name_ar'           => 'عيد الشرطة / ثورة 25 يناير',
            'name_en'           => 'Police Day / January 25 Revolution',
            'holiday_type'      => 'national',
            'date'              => $currentYear . '-01-25',
            'is_recurring'      => 1,
            'recurring_month'   => 1,
            'recurring_day'     => 25,
            'religion'          => null,
            'is_active'         => 1,
        ],
        [
            'name_ar'           => 'عيد تحرير سيناء',
            'name_en'           => 'Sinai Liberation Day',
            'holiday_type'      => 'national',
            'date'              => $currentYear . '-04-25',
            'is_recurring'      => 1,
            'recurring_month'   => 4,
            'recurring_day'     => 25,
            'religion'          => null,
            'is_active'         => 1,
        ],
        [
            'name_ar'           => 'عيد العمال',
            'name_en'           => 'Labour Day',
            'holiday_type'      => 'national',
            'date'              => $currentYear . '-05-01',
            'is_recurring'      => 1,
            'recurring_month'   => 5,
            'recurring_day'     => 1,
            'religion'          => null,
            'is_active'         => 1,
        ],
        [
            'name_ar'           => 'ثورة 30 يونيو',
            'name_en'           => 'June 30 Revolution',
            'holiday_type'      => 'national',
            'date'              => $currentYear . '-06-30',
            'is_recurring'      => 1,
            'recurring_month'   => 6,
            'recurring_day'     => 30,
            'religion'          => null,
            'is_active'         => 1,
        ],
        [
            'name_ar'           => 'ثورة 23 يوليو',
            'name_en'           => 'July 23 Revolution',
            'holiday_type'      => 'national',
            'date'              => $currentYear . '-07-23',
            'is_recurring'      => 1,
            'recurring_month'   => 7,
            'recurring_day'     => 23,
            'religion'          => null,
            'is_active'         => 1,
        ],
        [
            'name_ar'           => 'عيد القوات المسلحة (نصر أكتوبر)',
            'name_en'           => 'Armed Forces Day (October Victory)',
            'holiday_type'      => 'national',
            'date'              => $currentYear . '-10-06',
            'is_recurring'      => 1,
            'recurring_month'   => 10,
            'recurring_day'     => 6,
            'religion'          => null,
            'is_active'         => 1,
        ],

        // ── Islamic Religious Holidays (dates are approximate for current year) ──
        // These MUST be updated yearly by HR admin based on official lunar calendar announcements
        [
            'name_ar'           => 'عيد الفطر المبارك',
            'name_en'           => 'Eid al-Fitr',
            'holiday_type'      => 'religious',
            'date'              => $currentYear . '-03-30',
            'is_recurring'      => 0,
            'recurring_month'   => null,
            'recurring_day'     => null,
            'religion'          => null,
            'duration_days'     => 3,
            'is_active'         => 1,
        ],
        [
            'name_ar'           => 'وقفة عرفات وعيد الأضحى المبارك',
            'name_en'           => 'Arafat Day & Eid al-Adha',
            'holiday_type'      => 'religious',
            'date'              => $currentYear . '-06-06',
            'is_recurring'      => 0,
            'recurring_month'   => null,
            'recurring_day'     => null,
            'religion'          => null,
            'duration_days'     => 4,
            'is_active'         => 1,
        ],
        [
            'name_ar'           => 'رأس السنة الهجرية',
            'name_en'           => 'Islamic New Year',
            'holiday_type'      => 'religious',
            'date'              => $currentYear . '-06-26',
            'is_recurring'      => 0,
            'recurring_month'   => null,
            'recurring_day'     => null,
            'religion'          => null,
            'duration_days'     => 1,
            'is_active'         => 1,
        ],
        [
            'name_ar'           => 'المولد النبوي الشريف',
            'name_en'           => 'Prophet Muhammad\'s Birthday',
            'holiday_type'      => 'religious',
            'date'              => $currentYear . '-09-04',
            'is_recurring'      => 0,
            'recurring_month'   => null,
            'recurring_day'     => null,
            'religion'          => null,
            'duration_days'     => 1,
            'is_active'         => 1,
        ],

        // ── Coptic Christian Holidays ──
        [
            'name_ar'           => 'عيد الميلاد المجيد (القبطي)',
            'name_en'           => 'Coptic Christmas',
            'holiday_type'      => 'religious',
            'date'              => $currentYear . '-01-07',
            'is_recurring'      => 1,
            'recurring_month'   => 1,
            'recurring_day'     => 7,
            'religion'          => 'christian',
            'is_active'         => 1,
        ],
        [
            'name_ar'           => 'عيد القيامة المجيد (شم النسيم)',
            'name_en'           => 'Easter Monday (Sham el-Nessim)',
            'holiday_type'      => 'national',
            'date'              => $currentYear . '-04-21',
            'is_recurring'      => 0,
            'recurring_month'   => null,
            'recurring_day'     => null,
            'religion'          => null,
            'is_active'         => 1,
        ],
    ];

    foreach ($holidays as $h) {
        $existing = $db->selectOne(
            "SELECT id FROM hr_holidays WHERE name_ar = ? AND YEAR(`date`) = ?",
            [$h['name_ar'], $currentYear]
        );
        if ($existing) {
            continue;
        }

        $db->insert('hr_holidays', [
            'name_ar'          => $h['name_ar'],
            'name_en'          => $h['name_en'],
            'holiday_type'     => $h['holiday_type'],
            'date'             => $h['date'],
            'duration_days'    => $h['duration_days'] ?? 1,
            'is_recurring'     => $h['is_recurring'],
            'recurring_month'  => $h['recurring_month'] ?? null,
            'recurring_day'    => $h['recurring_day'] ?? null,
            'religion'         => $h['religion'] ?? null,
            'is_active'        => $h['is_active'],
            'created_at'       => $ts,
            'updated_at'       => $ts,
        ]);
    }
};
