<?php
declare(strict_types=1);

use App\Core\Database;

/**
 * Default Work Schedules
 *
 * 1. Standard 6-Day Week (Sat-Thu): 8h/day, 48h/week, Friday off
 * 2. Standard 5-Day Week (Sun-Thu): 9h/day, 45h/week, Fri+Sat off
 * 3. Night Shift: 7h/day per Art.80, rotating
 *
 * Egyptian Labour Law (Art. 80-85):
 *   - Max 8 hours/day or 48 hours/week (excluding breaks)
 *   - During Ramadan: 7 hours/day for Muslim employees
 *   - Night shift (7PM-7AM): 7 hours/day
 *   - At least 1 day off per week (minimum)
 */
return function (Database $db): void {
    $ts = date('Y-m-d H:i:s');

    $schedules = [
        [
            'code'                  => 'STD_6DAY',
            'name_ar'               => 'دوام 6 أيام (سبت - خميس)',
            'name_en'               => 'Standard 6-Day (Sat-Thu)',
            'schedule_type'         => 'fixed',
            'working_days_per_week' => 6,
            'hours_per_day'         => '8.00',
            'hours_per_week'        => '48.00',
            'ramadan_hours_per_day' => '7.00',
            'is_night_shift'        => 0,
            'start_time'            => '08:00:00',
            'end_time'              => '16:00:00',
            'break_duration_minutes'=> 60,
            'late_tolerance_minutes'=> 15,
            'early_leave_tolerance' => 15,
            'days_config_json'      => json_encode([
                'saturday'  => ['is_working' => true,  'start' => '08:00', 'end' => '16:00'],
                'sunday'    => ['is_working' => true,  'start' => '08:00', 'end' => '16:00'],
                'monday'    => ['is_working' => true,  'start' => '08:00', 'end' => '16:00'],
                'tuesday'   => ['is_working' => true,  'start' => '08:00', 'end' => '16:00'],
                'wednesday' => ['is_working' => true,  'start' => '08:00', 'end' => '16:00'],
                'thursday'  => ['is_working' => true,  'start' => '08:00', 'end' => '16:00'],
                'friday'    => ['is_working' => false, 'start' => null,    'end' => null],
            ]),
            'is_active' => 1,
        ],
        [
            'code'                  => 'STD_5DAY',
            'name_ar'               => 'دوام 5 أيام (أحد - خميس)',
            'name_en'               => 'Standard 5-Day (Sun-Thu)',
            'schedule_type'         => 'fixed',
            'working_days_per_week' => 5,
            'hours_per_day'         => '9.00',
            'hours_per_week'        => '45.00',
            'ramadan_hours_per_day' => '7.00',
            'is_night_shift'        => 0,
            'start_time'            => '08:00:00',
            'end_time'              => '17:00:00',
            'break_duration_minutes'=> 60,
            'late_tolerance_minutes'=> 15,
            'early_leave_tolerance' => 15,
            'days_config_json'      => json_encode([
                'saturday'  => ['is_working' => false, 'start' => null,    'end' => null],
                'sunday'    => ['is_working' => true,  'start' => '08:00', 'end' => '17:00'],
                'monday'    => ['is_working' => true,  'start' => '08:00', 'end' => '17:00'],
                'tuesday'   => ['is_working' => true,  'start' => '08:00', 'end' => '17:00'],
                'wednesday' => ['is_working' => true,  'start' => '08:00', 'end' => '17:00'],
                'thursday'  => ['is_working' => true,  'start' => '08:00', 'end' => '17:00'],
                'friday'    => ['is_working' => false, 'start' => null,    'end' => null],
            ]),
            'is_active' => 1,
        ],
        [
            'code'                  => 'NIGHT_SHIFT',
            'name_ar'               => 'دوام ليلي (7 ساعات)',
            'name_en'               => 'Night Shift (7 hours)',
            'schedule_type'         => 'rotating',
            'working_days_per_week' => 6,
            'hours_per_day'         => '7.00',
            'hours_per_week'        => '42.00',
            'ramadan_hours_per_day' => '7.00',
            'is_night_shift'        => 1,
            'start_time'            => '22:00:00',
            'end_time'              => '05:00:00',
            'break_duration_minutes'=> 30,
            'late_tolerance_minutes'=> 10,
            'early_leave_tolerance' => 10,
            'days_config_json'      => json_encode([
                'saturday'  => ['is_working' => true,  'start' => '22:00', 'end' => '05:00'],
                'sunday'    => ['is_working' => true,  'start' => '22:00', 'end' => '05:00'],
                'monday'    => ['is_working' => true,  'start' => '22:00', 'end' => '05:00'],
                'tuesday'   => ['is_working' => true,  'start' => '22:00', 'end' => '05:00'],
                'wednesday' => ['is_working' => true,  'start' => '22:00', 'end' => '05:00'],
                'thursday'  => ['is_working' => true,  'start' => '22:00', 'end' => '05:00'],
                'friday'    => ['is_working' => false, 'start' => null,    'end' => null],
            ]),
            'is_active' => 1,
        ],
    ];

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

        $db->insert('hr_work_schedules', [
            'code'                   => $sched['code'],
            'name_ar'                => $sched['name_ar'],
            'name_en'                => $sched['name_en'],
            'schedule_type'          => $sched['schedule_type'],
            'working_days_per_week'  => $sched['working_days_per_week'],
            'hours_per_day'          => $sched['hours_per_day'],
            'hours_per_week'         => $sched['hours_per_week'],
            'ramadan_hours_per_day'  => $sched['ramadan_hours_per_day'],
            'is_night_shift'         => $sched['is_night_shift'],
            'start_time'             => $sched['start_time'],
            'end_time'               => $sched['end_time'],
            'break_duration_minutes' => $sched['break_duration_minutes'],
            'late_tolerance_minutes' => $sched['late_tolerance_minutes'],
            'early_leave_tolerance'  => $sched['early_leave_tolerance'],
            'days_config_json'       => $sched['days_config_json'],
            'is_active'              => $sched['is_active'],
            'created_at'             => $ts,
            'updated_at'             => $ts,
        ]);
    }
};
