<?php

namespace Database\Seeders;

use App\Models\Holiday;
use Carbon\Carbon;
use Illuminate\Database\Seeder;

class HolidaySeeder extends Seeder
{
    /**
     * Run the database seeds.
     * Creates holidays for Indonesia in 2024, 2025, and 2026
     */
    public function run(): void
    {
        // Seed holidays for 2024, 2025, and 2026
        $this->seedHolidays2024();
        $this->seedHolidays2025();
        $this->seedHolidays2026();

        $this->command->info('Holiday seeder completed. Created holidays for 2024, 2025, and 2026.');
    }

    /**
     * Indonesian National Holidays 2024
     */
    private function seedHolidays2024(): void
    {
        // Clear existing holidays for 2024
        Holiday::whereYear('date', 2024)->delete();

        $nationalHolidays = [
            // Januari
            ['date' => '2024-01-01', 'name' => 'Tahun Baru 2024', 'description' => 'Tahun Baru Masehi'],

            // Februari
            ['date' => '2024-02-08', 'name' => 'Isra Miraj Nabi Muhammad SAW', 'description' => 'Hari Raya Islam'],
            ['date' => '2024-02-10', 'name' => 'Tahun Baru Imlek 2575', 'description' => 'Tahun Baru Imlek'],

            // Maret
            ['date' => '2024-03-11', 'name' => 'Hari Raya Nyepi', 'description' => 'Tahun Baru Saka 1946'],
            ['date' => '2024-03-29', 'name' => 'Wafat Isa Al Masih', 'description' => 'Hari Raya Kristen'],

            // April
            ['date' => '2024-04-10', 'name' => 'Hari Raya Idul Fitri 1445 H', 'description' => 'Hari Raya Idul Fitri'],
            ['date' => '2024-04-11', 'name' => 'Hari Raya Idul Fitri 1445 H', 'description' => 'Hari Raya Idul Fitri'],

            // Mei
            ['date' => '2024-05-01', 'name' => 'Hari Buruh Internasional', 'description' => 'Hari Buruh'],
            ['date' => '2024-05-09', 'name' => 'Kenaikan Isa Al Masih', 'description' => 'Hari Raya Kristen'],
            ['date' => '2024-05-23', 'name' => 'Hari Raya Waisak 2568 BE', 'description' => 'Hari Raya Buddha'],

            // Juni
            ['date' => '2024-06-01', 'name' => 'Hari Lahir Pancasila', 'description' => 'Hari Nasional'],
            ['date' => '2024-06-17', 'name' => 'Hari Raya Idul Adha 1445 H', 'description' => 'Hari Raya Islam'],

            // Juli
            ['date' => '2024-07-07', 'name' => 'Tahun Baru Islam 1446 H', 'description' => 'Tahun Baru Hijriyah'],

            // Agustus
            ['date' => '2024-08-17', 'name' => 'Hari Kemerdekaan RI ke-79', 'description' => 'Hari Kemerdekaan Republik Indonesia'],

            // September
            ['date' => '2024-09-16', 'name' => 'Maulid Nabi Muhammad SAW', 'description' => 'Hari Raya Islam'],

            // Desember
            ['date' => '2024-12-25', 'name' => 'Hari Raya Natal', 'description' => 'Hari Raya Kristen'],
        ];

        // Cuti Bersama 2024
        $jointLeaves = [
            // Cuti bersama Imlek
            ['date' => '2024-02-09', 'name' => 'Cuti Bersama Tahun Baru Imlek', 'description' => 'Cuti Bersama'],

            // Cuti bersama Nyepi
            ['date' => '2024-03-12', 'name' => 'Cuti Bersama Nyepi', 'description' => 'Cuti Bersama'],

            // Cuti bersama Idul Fitri
            ['date' => '2024-04-08', 'name' => 'Cuti Bersama Idul Fitri', 'description' => 'Cuti Bersama'],
            ['date' => '2024-04-09', 'name' => 'Cuti Bersama Idul Fitri', 'description' => 'Cuti Bersama'],
            ['date' => '2024-04-12', 'name' => 'Cuti Bersama Idul Fitri', 'description' => 'Cuti Bersama'],
            ['date' => '2024-04-15', 'name' => 'Cuti Bersama Idul Fitri', 'description' => 'Cuti Bersama'],

            // Cuti bersama Kenaikan Isa Al Masih
            ['date' => '2024-05-10', 'name' => 'Cuti Bersama Kenaikan Isa Al Masih', 'description' => 'Cuti Bersama'],

            // Cuti bersama Waisak
            ['date' => '2024-05-24', 'name' => 'Cuti Bersama Waisak', 'description' => 'Cuti Bersama'],

            // Cuti bersama Idul Adha
            ['date' => '2024-06-18', 'name' => 'Cuti Bersama Idul Adha', 'description' => 'Cuti Bersama'],

            // Cuti bersama Natal dan Tahun Baru
            ['date' => '2024-12-26', 'name' => 'Cuti Bersama Natal', 'description' => 'Cuti Bersama'],
        ];

        $this->insertHolidays($nationalHolidays);
        $this->insertHolidays($jointLeaves);
        $this->generateWeekendHolidays(2024);

        $this->command->info('Created holidays for 2024.');
    }

    /**
     * Indonesian National Holidays 2025
     * Sesuai data dari file Excel PMC 2025
     */
    private function seedHolidays2025(): void
    {
        // Clear existing holidays for 2025
        Holiday::whereYear('date', 2025)->delete();

        $nationalHolidays = [
            // Januari
            ['date' => '2025-01-01', 'name' => 'Tahun Baru 2025', 'description' => 'Tahun Baru Masehi'],
            ['date' => '2025-01-27', 'name' => 'Isra Miraj Nabi Muhammad SAW', 'description' => 'Hari Raya Islam'],
            ['date' => '2025-01-29', 'name' => 'Tahun Baru Imlek 2576', 'description' => 'Tahun Baru Imlek'],

            // Maret
            ['date' => '2025-03-29', 'name' => 'Hari Raya Nyepi', 'description' => 'Tahun Baru Saka 1947'],
            ['date' => '2025-03-30', 'name' => 'Hari Raya Idul Fitri 1446 H', 'description' => 'Hari Raya Idul Fitri'],
            ['date' => '2025-03-31', 'name' => 'Hari Raya Idul Fitri 1446 H', 'description' => 'Hari Raya Idul Fitri'],

            // April
            ['date' => '2025-04-01', 'name' => 'Hari Raya Idul Fitri 1446 H', 'description' => 'Hari Raya Idul Fitri'],
            ['date' => '2025-04-18', 'name' => 'Wafat Isa Al Masih', 'description' => 'Hari Raya Kristen'],

            // Mei
            ['date' => '2025-05-01', 'name' => 'Hari Buruh Internasional', 'description' => 'Hari Buruh'],
            ['date' => '2025-05-12', 'name' => 'Hari Raya Waisak 2569 BE', 'description' => 'Hari Raya Buddha'],
            ['date' => '2025-05-29', 'name' => 'Kenaikan Isa Al Masih', 'description' => 'Hari Raya Kristen'],

            // Juni
            ['date' => '2025-06-06', 'name' => 'Hari Lahir Pancasila', 'description' => 'Hari Nasional'],
            ['date' => '2025-06-27', 'name' => 'Tahun Baru Islam 1447 H', 'description' => 'Tahun Baru Hijriyah'],

            // Agustus
            ['date' => '2025-08-17', 'name' => 'Hari Kemerdekaan RI ke-80', 'description' => 'Hari Kemerdekaan Republik Indonesia'],

            // September
            ['date' => '2025-09-05', 'name' => 'Maulid Nabi Muhammad SAW', 'description' => 'Hari Raya Islam'],

            // Desember
            ['date' => '2025-12-25', 'name' => 'Hari Raya Natal', 'description' => 'Hari Raya Kristen'],
        ];

        // Cuti Bersama 2025 - sesuai data Excel PMC
        $jointLeaves = [
            // Cuti bersama Imlek
            ['date' => '2025-01-28', 'name' => 'Cuti Bersama Imlek', 'description' => 'Cuti Bersama'],

            // Cuti bersama Idul Fitri
            ['date' => '2025-03-28', 'name' => 'Cuti Bersama Idul Fitri', 'description' => 'Cuti Bersama'],
            ['date' => '2025-04-02', 'name' => 'Cuti Bersama Idul Fitri', 'description' => 'Cuti Bersama'],
            ['date' => '2025-04-03', 'name' => 'Cuti Bersama Idul Fitri', 'description' => 'Cuti Bersama'],
            ['date' => '2025-04-04', 'name' => 'Cuti Bersama Idul Fitri', 'description' => 'Cuti Bersama'],
            ['date' => '2025-04-07', 'name' => 'Cuti Bersama Idul Fitri', 'description' => 'Cuti Bersama'],

            // Cuti bersama Waisak
            ['date' => '2025-05-13', 'name' => 'Cuti Bersama Waisak', 'description' => 'Cuti Bersama'],

            // Cuti bersama Kenaikan Isa Al Masih
            ['date' => '2025-05-30', 'name' => 'Cuti Bersama Kenaikan Isa Al Masih', 'description' => 'Cuti Bersama'],

            // Cuti bersama Hari Lahir Pancasila
            ['date' => '2025-06-09', 'name' => 'Cuti Bersama Hari Lahir Pancasila', 'description' => 'Cuti Bersama'],

            // Cuti bersama Hari Kemerdekaan
            ['date' => '2025-08-18', 'name' => 'Cuti Bersama Hari Kemerdekaan', 'description' => 'Cuti Bersama'],

            // Cuti bersama Natal
            ['date' => '2025-12-26', 'name' => 'Cuti Bersama Natal', 'description' => 'Cuti Bersama'],
        ];

        $this->insertHolidays($nationalHolidays);
        $this->insertHolidays($jointLeaves);
        $this->generateWeekendHolidays(2025);

        $this->command->info('Created holidays for 2025.');
    }

    /**
     * Indonesian National Holidays 2026
     * Sesuai Kalender Libur & Cuti Bersama Indonesia 2026
     */
    private function seedHolidays2026(): void
    {
        // Clear existing holidays for 2026
        Holiday::whereYear('date', 2026)->delete();

        $nationalHolidays = [
            // Januari
            ['date' => '2026-01-01', 'name' => 'Tahun Baru 2026', 'description' => 'Tahun Baru Masehi'],
            ['date' => '2026-01-16', 'name' => 'Isra Miraj Nabi Muhammad SAW', 'description' => 'Hari Raya Islam'],

            // Februari
            ['date' => '2026-02-17', 'name' => 'Tahun Baru Imlek 2577', 'description' => 'Tahun Baru Imlek'],

            // Maret
            ['date' => '2026-03-18', 'name' => 'Cuti Nyepi', 'description' => 'Cuti Bersama Nyepi'],
            ['date' => '2026-03-19', 'name' => 'Hari Raya Nyepi', 'description' => 'Tahun Baru Saka 1948'],
            ['date' => '2026-03-20', 'name' => 'Cuti Idul Fitri', 'description' => 'Cuti Bersama Idul Fitri'],
            ['date' => '2026-03-21', 'name' => 'Hari Raya Idul Fitri 1447 H', 'description' => 'Hari Raya Idul Fitri'],
            ['date' => '2026-03-22', 'name' => 'Hari Raya Idul Fitri 1447 H', 'description' => 'Hari Raya Idul Fitri'],
            ['date' => '2026-03-23', 'name' => 'Cuti Idul Fitri', 'description' => 'Cuti Bersama Idul Fitri'],
            ['date' => '2026-03-24', 'name' => 'Cuti Idul Fitri', 'description' => 'Cuti Bersama Idul Fitri'],

            // April
            ['date' => '2026-04-03', 'name' => 'Wafat Isa Al Masih', 'description' => 'Hari Raya Kristen'],
            ['date' => '2026-04-05', 'name' => 'Paskah', 'description' => 'Hari Raya Kristen'],

            // Mei
            ['date' => '2026-05-01', 'name' => 'Hari Buruh Internasional', 'description' => 'Hari Buruh'],
            ['date' => '2026-05-14', 'name' => 'Kenaikan Isa Al Masih', 'description' => 'Hari Raya Kristen'],
            ['date' => '2026-05-15', 'name' => 'Cuti Kenaikan Isa Almasih', 'description' => 'Cuti Bersama'],
            ['date' => '2026-05-27', 'name' => 'Hari Raya Idul Adha 1447 H', 'description' => 'Hari Raya Islam'],
            ['date' => '2026-05-28', 'name' => 'Cuti Idul Adha', 'description' => 'Cuti Bersama Idul Adha'],
            ['date' => '2026-05-31', 'name' => 'Hari Raya Waisak 2570 BE', 'description' => 'Hari Raya Buddha'],

            // Juni
            ['date' => '2026-06-01', 'name' => 'Hari Lahir Pancasila', 'description' => 'Hari Nasional'],
            ['date' => '2026-06-16', 'name' => 'Tahun Baru Islam 1448 H', 'description' => 'Tahun Baru Hijriyah'],

            // Agustus
            ['date' => '2026-08-17', 'name' => 'Hari Kemerdekaan RI ke-81', 'description' => 'Hari Kemerdekaan Republik Indonesia'],
            ['date' => '2026-08-25', 'name' => 'Maulid Nabi Muhammad SAW', 'description' => 'Hari Raya Islam'],

            // Desember
            ['date' => '2026-12-24', 'name' => 'Cuti Natal', 'description' => 'Cuti Bersama Natal'],
            ['date' => '2026-12-25', 'name' => 'Hari Raya Natal', 'description' => 'Hari Raya Kristen'],
        ];

        // Cuti Bersama 2026 - sesuai kalender resmi
        $jointLeaves = [
            // Cuti bersama Imlek (Long Weekend 14-17 Feb)
            ['date' => '2026-02-16', 'name' => 'Cuti Imlek', 'description' => 'Cuti Bersama Imlek'],

            // Long Weekend April (3-5 Apr: Jumat-Minggu)
            ['date' => '2026-04-04', 'name' => 'Cuti Wafat Isa Almasih', 'description' => 'Cuti Bersama'],
        ];

        $this->insertHolidays($nationalHolidays);
        $this->insertHolidays($jointLeaves);
        $this->generateWeekendHolidays(2026);

        $this->command->info('Created holidays for 2026.');
    }

    /**
     * Insert holidays into database
     * Menggunakan firstOrCreate untuk mencegah duplikat
     */
    private function insertHolidays(array $holidays): void
    {
        foreach ($holidays as $holiday) {
            Holiday::firstOrCreate(
                [
                    'date' => $holiday['date'],
                    'company_id' => null,
                ],
                [
                    'name' => $holiday['name'],
                    'description' => $holiday['description'],
                    'is_recurring' => false,
                    'is_active' => true,
                ]
            );
        }
    }

    /**
     * Generate Saturday and Sunday as holidays for the entire year
     */
    private function generateWeekendHolidays(int $year): void
    {
        $startDate = Carbon::createFromDate($year, 1, 1);
        $endDate = Carbon::createFromDate($year, 12, 31);

        $saturdays = 0;
        $sundays = 0;

        while ($startDate->lte($endDate)) {
            if ($startDate->isSaturday()) {
                Holiday::firstOrCreate(
                    [
                        'date' => $startDate->format('Y-m-d'),
                        'company_id' => null,
                    ],
                    [
                        'name' => 'Hari Sabtu',
                        'description' => 'Akhir Pekan',
                        'is_recurring' => false,
                        'is_active' => true,
                    ]
                );
                $saturdays++;
            }

            if ($startDate->isSunday()) {
                Holiday::firstOrCreate(
                    [
                        'date' => $startDate->format('Y-m-d'),
                        'company_id' => null,
                    ],
                    [
                        'name' => 'Hari Minggu',
                        'description' => 'Akhir Pekan',
                        'is_recurring' => false,
                        'is_active' => true,
                    ]
                );
                $sundays++;
            }

            $startDate->addDay();
        }

        $this->command->info("Generated {$saturdays} Saturdays and {$sundays} Sundays for {$year}.");
    }
}
