<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateSubjectTargetHours extends Migration
{
    public function up()
    {
        // Tabel master target jam per mapel per tingkat
        $this->forge->addField([
            'id' => [
                'type' => 'INT',
                'unsigned' => true,
                'auto_increment' => true,
            ],
            'subject_id' => [
                'type' => 'INT',
                'unsigned' => true,
                'comment' => 'ID mata pelajaran (school_subjects)',
            ],
            'grade_id' => [
                'type' => 'INT',
                'unsigned' => true,
                'comment' => 'ID tingkat (grades)',
            ],
            'target_hours' => [
                'type' => 'TINYINT',
                'unsigned' => true,
                'default' => 0,
                'comment' => 'Target jam per minggu untuk mapel ini di tingkat ini',
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['subject_id', 'grade_id']);
        $this->forge->createTable('subject_target_hours', true);

        // Insert default target berdasarkan struktur kurikulum
        $defaultTargets = [
            // Mapel: Al-Qur'an Hadis (ID 1)
            ['subject_id' => 1, 'grade_id' => 1, 'target_hours' => 2], // VII
            ['subject_id' => 1, 'grade_id' => 2, 'target_hours' => 2], // VIII
            ['subject_id' => 1, 'grade_id' => 3, 'target_hours' => 2], // IX
            // Mapel: Akidah Akhlak (ID 2)
            ['subject_id' => 2, 'grade_id' => 1, 'target_hours' => 2],
            ['subject_id' => 2, 'grade_id' => 2, 'target_hours' => 2],
            ['subject_id' => 2, 'grade_id' => 3, 'target_hours' => 2],
            // Mapel: Fikih (ID 3)
            ['subject_id' => 3, 'grade_id' => 1, 'target_hours' => 2],
            ['subject_id' => 3, 'grade_id' => 2, 'target_hours' => 2],
            ['subject_id' => 3, 'grade_id' => 3, 'target_hours' => 2],
            // Mapel: SKI (ID 4)
            ['subject_id' => 4, 'grade_id' => 1, 'target_hours' => 2],
            ['subject_id' => 4, 'grade_id' => 2, 'target_hours' => 2],
            ['subject_id' => 4, 'grade_id' => 3, 'target_hours' => 2],
            // Mapel: Bahasa Arab (ID 5)
            ['subject_id' => 5, 'grade_id' => 1, 'target_hours' => 2],
            ['subject_id' => 5, 'grade_id' => 2, 'target_hours' => 2],
            ['subject_id' => 5, 'grade_id' => 3, 'target_hours' => 2],
            // Mapel: Bahasa Indonesia (ID 6)
            ['subject_id' => 6, 'grade_id' => 1, 'target_hours' => 4],
            ['subject_id' => 6, 'grade_id' => 2, 'target_hours' => 4],
            ['subject_id' => 6, 'grade_id' => 3, 'target_hours' => 4],
            // Mapel: Bahasa Inggris (ID 7)
            ['subject_id' => 7, 'grade_id' => 1, 'target_hours' => 4],
            ['subject_id' => 7, 'grade_id' => 2, 'target_hours' => 4],
            ['subject_id' => 7, 'grade_id' => 3, 'target_hours' => 4],
            // Mapel: Matematika (ID 8)
            ['subject_id' => 8, 'grade_id' => 1, 'target_hours' => 5],
            ['subject_id' => 8, 'grade_id' => 2, 'target_hours' => 5],
            ['subject_id' => 8, 'grade_id' => 3, 'target_hours' => 5],
            // Mapel: IPA (ID 9)
            ['subject_id' => 9, 'grade_id' => 1, 'target_hours' => 4],
            ['subject_id' => 9, 'grade_id' => 2, 'target_hours' => 4],
            ['subject_id' => 9, 'grade_id' => 3, 'target_hours' => 4],
            // Mapel: IPS (ID 10)
            ['subject_id' => 10, 'grade_id' => 1, 'target_hours' => 3],
            ['subject_id' => 10, 'grade_id' => 2, 'target_hours' => 3],
            ['subject_id' => 10, 'grade_id' => 3, 'target_hours' => 3],
            // Mapel: PJOK (ID 11)
            ['subject_id' => 11, 'grade_id' => 1, 'target_hours' => 2],
            ['subject_id' => 11, 'grade_id' => 2, 'target_hours' => 2],
            ['subject_id' => 11, 'grade_id' => 3, 'target_hours' => 2],
            // Mapel: Seni Budaya (ID 12)
            ['subject_id' => 12, 'grade_id' => 1, 'target_hours' => 2],
            ['subject_id' => 12, 'grade_id' => 2, 'target_hours' => 2],
            ['subject_id' => 12, 'grade_id' => 3, 'target_hours' => 2],
            // Mapel: Informatika (ID 13)
            ['subject_id' => 13, 'grade_id' => 1, 'target_hours' => 2],
            ['subject_id' => 13, 'grade_id' => 2, 'target_hours' => 2],
            ['subject_id' => 13, 'grade_id' => 3, 'target_hours' => 2],
            // Mapel: Tahfidzul Qur'an (ID 14)
            ['subject_id' => 14, 'grade_id' => 1, 'target_hours' => 1],
            ['subject_id' => 14, 'grade_id' => 2, 'target_hours' => 1],
            ['subject_id' => 14, 'grade_id' => 3, 'target_hours' => 1],
            // Mapel: Bahasa Jawa (ID 15)
            ['subject_id' => 15, 'grade_id' => 1, 'target_hours' => 1],
            ['subject_id' => 15, 'grade_id' => 2, 'target_hours' => 1],
            ['subject_id' => 15, 'grade_id' => 3, 'target_hours' => 1],
        ];

        foreach ($defaultTargets as $target) {
            $this->db->table('subject_target_hours')->insert($target);
        }
    }

    public function down()
    {
        $this->forge->dropTable('subject_target_hours', true);
    }
}