<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class SeedDefaultMasterData extends Migration
{
    public function up()
    {
        $this->seedCalendarCategories();
        $this->seedSubjects();
    }

    public function down()
    {
        // Tidak menghapus data saat rollback agar data pengguna tidak hilang
    }

    // =========================================================
    // KATEGORI KALENDER AKADEMIK
    // =========================================================
    private function seedCalendarCategories(): void
    {
        // Lewati jika sudah ada data
        if ($this->db->table('calendar_categories')->where('deleted_at', null)->countAllResults() > 0) {
            return;
        }

        $now = date('Y-m-d H:i:s');

        $categories = [
            [
                'name'       => 'Hari Libur Nasional/Keagamaan',
                'code'       => 'HL',
                'color'      => '#e53e3e',
                'is_libur'   => 1,
                'sort_order' => 1,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name'       => 'Libur Semester',
                'code'       => 'LS',
                'color'      => '#dd6b20',
                'is_libur'   => 1,
                'sort_order' => 2,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name'       => 'Kegiatan Belajar Mengajar',
                'code'       => 'KBM',
                'color'      => '#38a169',
                'is_libur'   => 0,
                'sort_order' => 3,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name'       => 'Penilaian Tengah Semester',
                'code'       => 'PTS',
                'color'      => '#3182ce',
                'is_libur'   => 0,
                'sort_order' => 4,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name'       => 'Penilaian Akhir Semester',
                'code'       => 'PAS',
                'color'      => '#805ad5',
                'is_libur'   => 0,
                'sort_order' => 5,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name'       => 'Ujian Madrasah',
                'code'       => 'UM',
                'color'      => '#d53f8c',
                'is_libur'   => 0,
                'sort_order' => 6,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name'       => 'Penerimaan Peserta Didik Baru',
                'code'       => 'PPDB',
                'color'      => '#319795',
                'is_libur'   => 0,
                'sort_order' => 7,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name'       => 'Penerimaan/Pembagian Rapor',
                'code'       => 'PPR',
                'color'      => '#d69e2e',
                'is_libur'   => 0,
                'sort_order' => 8,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name'       => 'Kegiatan Ekstrakurikuler',
                'code'       => 'EKS',
                'color'      => '#00b5d8',
                'is_libur'   => 0,
                'sort_order' => 9,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name'       => 'Kegiatan Khusus/Lainnya',
                'code'       => 'KK',
                'color'      => '#718096',
                'is_libur'   => 0,
                'sort_order' => 10,
                'created_at' => $now,
                'updated_at' => $now,
            ],
        ];

        $this->db->table('calendar_categories')->insertBatch($categories);
    }

    // =========================================================
    // MASTER MATA PELAJARAN
    // =========================================================
    private function seedSubjects(): void
    {
        // Lewati jika sudah ada data
        if ($this->db->table('subjects')->countAll() > 0) {
            return;
        }

        $subjects = [
            // ==================== MI (Madrasah Ibtidaiyah) ====================
            ['name' => 'Pendidikan Pancasila',    'code' => 'PP',    'level' => 'MI',  'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Bahasa Indonesia',         'code' => 'BIN',   'level' => 'MI',  'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Matematika',               'code' => 'MTK',   'level' => 'MI',  'group' => 'Umum',              'is_core' => 1],
            ['name' => 'IPAS',                     'code' => 'IPAS',  'level' => 'MI',  'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Seni dan Budaya',          'code' => 'SB',    'level' => 'MI',  'group' => 'Umum',              'is_core' => 1],
            ['name' => 'PJOK',                     'code' => 'PJOK',  'level' => 'MI',  'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Al-Qur\'an Hadis',         'code' => 'QH',    'level' => 'MI',  'group' => 'Keagamaan',         'is_core' => 1],
            ['name' => 'Akidah Akhlak',            'code' => 'AA',    'level' => 'MI',  'group' => 'Keagamaan',         'is_core' => 1],
            ['name' => 'Fikih',                    'code' => 'FIQ',   'level' => 'MI',  'group' => 'Keagamaan',         'is_core' => 1],
            ['name' => 'Sejarah Kebudayaan Islam', 'code' => 'SKI',   'level' => 'MI',  'group' => 'Keagamaan',         'is_core' => 1],
            ['name' => 'Bahasa Arab',              'code' => 'BA',    'level' => 'MI',  'group' => 'Keagamaan',         'is_core' => 1],

            // ==================== MTs (Madrasah Tsanawiyah) ====================
            ['name' => 'Pendidikan Pancasila',    'code' => 'PP',    'level' => 'MTs', 'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Bahasa Indonesia',         'code' => 'BIN',   'level' => 'MTs', 'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Matematika',               'code' => 'MTK',   'level' => 'MTs', 'group' => 'Umum',              'is_core' => 1],
            ['name' => 'IPA',                      'code' => 'IPA',   'level' => 'MTs', 'group' => 'Umum',              'is_core' => 1],
            ['name' => 'IPS',                      'code' => 'IPS',   'level' => 'MTs', 'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Bahasa Inggris',           'code' => 'BIG',   'level' => 'MTs', 'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Informatika',              'code' => 'INF',   'level' => 'MTs', 'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Seni dan Budaya',          'code' => 'SB',    'level' => 'MTs', 'group' => 'Umum',              'is_core' => 1],
            ['name' => 'PJOK',                     'code' => 'PJOK',  'level' => 'MTs', 'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Al-Qur\'an Hadis',         'code' => 'QH',    'level' => 'MTs', 'group' => 'Keagamaan',         'is_core' => 1],
            ['name' => 'Akidah Akhlak',            'code' => 'AA',    'level' => 'MTs', 'group' => 'Keagamaan',         'is_core' => 1],
            ['name' => 'Fikih',                    'code' => 'FIQ',   'level' => 'MTs', 'group' => 'Keagamaan',         'is_core' => 1],
            ['name' => 'Sejarah Kebudayaan Islam', 'code' => 'SKI',   'level' => 'MTs', 'group' => 'Keagamaan',         'is_core' => 1],
            ['name' => 'Bahasa Arab',              'code' => 'BA',    'level' => 'MTs', 'group' => 'Keagamaan',         'is_core' => 1],

            // ==================== MA (Madrasah Aliyah) ====================
            ['name' => 'Pendidikan Pancasila',    'code' => 'PP',    'level' => 'MA',  'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Bahasa Indonesia',         'code' => 'BIN',   'level' => 'MA',  'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Bahasa Inggris',           'code' => 'BIG',   'level' => 'MA',  'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Matematika',               'code' => 'MTK',   'level' => 'MA',  'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Sejarah Indonesia',        'code' => 'SJ',    'level' => 'MA',  'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Informatika',              'code' => 'INF',   'level' => 'MA',  'group' => 'Umum',              'is_core' => 1],
            ['name' => 'PJOK',                     'code' => 'PJOK',  'level' => 'MA',  'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Seni dan Budaya',          'code' => 'SB',    'level' => 'MA',  'group' => 'Umum',              'is_core' => 1],
            ['name' => 'Al-Qur\'an Hadis',         'code' => 'QH',    'level' => 'MA',  'group' => 'Keagamaan',         'is_core' => 1],
            ['name' => 'Akidah Akhlak',            'code' => 'AA',    'level' => 'MA',  'group' => 'Keagamaan',         'is_core' => 1],
            ['name' => 'Fikih',                    'code' => 'FIQ',   'level' => 'MA',  'group' => 'Keagamaan',         'is_core' => 1],
            ['name' => 'Sejarah Kebudayaan Islam', 'code' => 'SKI',   'level' => 'MA',  'group' => 'Keagamaan',         'is_core' => 1],
            ['name' => 'Bahasa Arab',              'code' => 'BA',    'level' => 'MA',  'group' => 'Keagamaan',         'is_core' => 1],
            ['name' => 'Biologi',                  'code' => 'BIO',   'level' => 'MA',  'group' => 'Pilihan IPA',       'is_core' => 0],
            ['name' => 'Fisika',                   'code' => 'FIS',   'level' => 'MA',  'group' => 'Pilihan IPA',       'is_core' => 0],
            ['name' => 'Kimia',                    'code' => 'KIM',   'level' => 'MA',  'group' => 'Pilihan IPA',       'is_core' => 0],
            ['name' => 'Ekonomi',                  'code' => 'EKO',   'level' => 'MA',  'group' => 'Pilihan IPS',       'is_core' => 0],
            ['name' => 'Geografi',                 'code' => 'GEO',   'level' => 'MA',  'group' => 'Pilihan IPS',       'is_core' => 0],
            ['name' => 'Sosiologi',                'code' => 'SOS',   'level' => 'MA',  'group' => 'Pilihan IPS',       'is_core' => 0],
            ['name' => 'Antropologi',              'code' => 'ANT',   'level' => 'MA',  'group' => 'Pilihan IPS',       'is_core' => 0],
            ['name' => 'Bahasa Asing',             'code' => 'BAING', 'level' => 'MA',  'group' => 'Pilihan Lintas Minat', 'is_core' => 0],

            // ==================== Tambahan (Semua Jenjang) ====================
            ['name' => 'Muatan Lokal', 'code' => 'MULOK', 'level' => 'ALL', 'group' => 'Tambahan', 'is_core' => 0],
            ['name' => 'P5RA',         'code' => 'P5RA',  'level' => 'ALL', 'group' => 'Tambahan', 'is_core' => 0],
        ];

        $this->db->table('subjects')->insertBatch($subjects);
    }
}
