<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class SupportGeneralSchoolLevels extends Migration
{
    public function up()
    {
        // Keep existing school codes while allowing TK and general schools.
        $this->db->query("ALTER TABLE school_settings MODIFY school_type VARCHAR(10) NOT NULL DEFAULT 'MTs'");

        $subjects = [
            ['name' => 'Nilai Agama dan Budi Pekerti', 'code' => 'NABP', 'level' => 'TK',  'group' => 'TK', 'is_core' => 1],
            ['name' => 'Jati Diri', 'code' => 'JD', 'level' => 'TK', 'group' => 'TK', 'is_core' => 1],
            ['name' => 'Dasar-Dasar Literasi, Matematika, Sains, Teknologi, Rekayasa, dan Seni', 'code' => 'DLM', 'level' => 'TK', 'group' => 'TK', 'is_core' => 1],

            ['name' => 'Pendidikan Agama', 'code' => 'PA', 'level' => 'SD', 'group' => 'SD', 'is_core' => 1],
            ['name' => 'Pendidikan Pancasila', 'code' => 'PP', 'level' => 'SD', 'group' => 'SD', 'is_core' => 1],
            ['name' => 'Bahasa Indonesia', 'code' => 'BIN', 'level' => 'SD', 'group' => 'SD', 'is_core' => 1],
            ['name' => 'Matematika', 'code' => 'MTK', 'level' => 'SD', 'group' => 'SD', 'is_core' => 1],
            ['name' => 'IPAS', 'code' => 'IPAS', 'level' => 'SD', 'group' => 'SD', 'is_core' => 1],
            ['name' => 'PJOK', 'code' => 'PJOK', 'level' => 'SD', 'group' => 'SD', 'is_core' => 1],
            ['name' => 'Seni', 'code' => 'SENI', 'level' => 'SD', 'group' => 'SD', 'is_core' => 1],
            ['name' => 'Bahasa Inggris', 'code' => 'BIG', 'level' => 'SD', 'group' => 'SD', 'is_core' => 0],

            ['name' => 'Pendidikan Agama', 'code' => 'PA', 'level' => 'SMP', 'group' => 'SMP', 'is_core' => 1],
            ['name' => 'Pendidikan Pancasila', 'code' => 'PP', 'level' => 'SMP', 'group' => 'SMP', 'is_core' => 1],
            ['name' => 'Bahasa Indonesia', 'code' => 'BIN', 'level' => 'SMP', 'group' => 'SMP', 'is_core' => 1],
            ['name' => 'Matematika', 'code' => 'MTK', 'level' => 'SMP', 'group' => 'SMP', 'is_core' => 1],
            ['name' => 'IPA', 'code' => 'IPA', 'level' => 'SMP', 'group' => 'SMP', 'is_core' => 1],
            ['name' => 'IPS', 'code' => 'IPS', 'level' => 'SMP', 'group' => 'SMP', 'is_core' => 1],
            ['name' => 'Bahasa Inggris', 'code' => 'BIG', 'level' => 'SMP', 'group' => 'SMP', 'is_core' => 1],
            ['name' => 'Informatika', 'code' => 'INF', 'level' => 'SMP', 'group' => 'SMP', 'is_core' => 1],
            ['name' => 'PJOK', 'code' => 'PJOK', 'level' => 'SMP', 'group' => 'SMP', 'is_core' => 1],
            ['name' => 'Seni', 'code' => 'SENI', 'level' => 'SMP', 'group' => 'SMP', 'is_core' => 1],

            ['name' => 'Pendidikan Agama', 'code' => 'PA', 'level' => 'SMA', 'group' => 'SMA', 'is_core' => 1],
            ['name' => 'Pendidikan Pancasila', 'code' => 'PP', 'level' => 'SMA', 'group' => 'SMA', 'is_core' => 1],
            ['name' => 'Bahasa Indonesia', 'code' => 'BIN', 'level' => 'SMA', 'group' => 'SMA', 'is_core' => 1],
            ['name' => 'Matematika', 'code' => 'MTK', 'level' => 'SMA', 'group' => 'SMA', 'is_core' => 1],
            ['name' => 'Bahasa Inggris', 'code' => 'BIG', 'level' => 'SMA', 'group' => 'SMA', 'is_core' => 1],
            ['name' => 'Sejarah', 'code' => 'SJ', 'level' => 'SMA', 'group' => 'SMA', 'is_core' => 1],
            ['name' => 'Informatika', 'code' => 'INF', 'level' => 'SMA', 'group' => 'SMA', 'is_core' => 1],
            ['name' => 'PJOK', 'code' => 'PJOK', 'level' => 'SMA', 'group' => 'SMA', 'is_core' => 1],
            ['name' => 'Biologi', 'code' => 'BIO', 'level' => 'SMA', 'group' => 'SMA', 'is_core' => 0],
            ['name' => 'Fisika', 'code' => 'FIS', 'level' => 'SMA', 'group' => 'SMA', 'is_core' => 0],
            ['name' => 'Kimia', 'code' => 'KIM', 'level' => 'SMA', 'group' => 'SMA', 'is_core' => 0],
            ['name' => 'Ekonomi', 'code' => 'EKO', 'level' => 'SMA', 'group' => 'SMA', 'is_core' => 0],

            ['name' => 'Pendidikan Agama', 'code' => 'PA', 'level' => 'SMK', 'group' => 'SMK', 'is_core' => 1],
            ['name' => 'Pendidikan Pancasila', 'code' => 'PP', 'level' => 'SMK', 'group' => 'SMK', 'is_core' => 1],
            ['name' => 'Bahasa Indonesia', 'code' => 'BIN', 'level' => 'SMK', 'group' => 'SMK', 'is_core' => 1],
            ['name' => 'Matematika', 'code' => 'MTK', 'level' => 'SMK', 'group' => 'SMK', 'is_core' => 1],
            ['name' => 'Bahasa Inggris', 'code' => 'BIG', 'level' => 'SMK', 'group' => 'SMK', 'is_core' => 1],
            ['name' => 'Informatika', 'code' => 'INF', 'level' => 'SMK', 'group' => 'SMK', 'is_core' => 1],
            ['name' => 'Projek Kreatif dan Kewirausahaan', 'code' => 'PKK', 'level' => 'SMK', 'group' => 'SMK', 'is_core' => 1],
            ['name' => 'Dasar-Dasar Program Keahlian', 'code' => 'DPK', 'level' => 'SMK', 'group' => 'SMK', 'is_core' => 1],
            ['name' => 'Konsentrasi Keahlian', 'code' => 'KK', 'level' => 'SMK', 'group' => 'SMK', 'is_core' => 1],
            ['name' => 'Praktik Kerja Lapangan', 'code' => 'PKL', 'level' => 'SMK', 'group' => 'SMK', 'is_core' => 0],
        ];

        foreach ($subjects as $subject) {
            $exists = $this->db->table('subjects')
                ->where('name', $subject['name'])
                ->where('level', $subject['level'])
                ->countAllResults();

            if (!$exists) {
                $this->db->table('subjects')->insert($subject);
            }
        }
    }

    public function down()
    {
        // Do not remove subject rows or change existing school settings on rollback.
    }
}
