<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateAscSchedules extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'import_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],

            // ── DATA KELAS ─────────────────────────────────────
            'class_asc_id' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
            ],
            'class_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
                'comment'    => 'Misal: 7A',
            ],
            'grade_level' => [
                'type'       => 'TINYINT',
                'constraint' => 2,
                'null'       => true,
                'comment'    => '7, 8, atau 9',
            ],

            // ── DATA MAPEL ─────────────────────────────────────
            'subject_asc_id' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
            ],
            'subject_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'subject_code' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'comment'    => 'Kode singkat: MAT, BIG, IPA',
            ],

            // ── DATA GURU ──────────────────────────────────────
            'teacher_asc_id' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
            ],
            'teacher_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'teacher_code' => [
                'type'       => 'VARCHAR',
                'constraint' => 10,
                'comment'    => 'Short code guru: 01, 02, 32',
            ],
            'teacher_color' => [
                'type'       => 'VARCHAR',
                'constraint' => 10,
                'default'    => '#CCCCCC',
                'comment'    => 'Warna unik guru dari XML',
            ],

            // ── DATA WAKTU ─────────────────────────────────────
            'day_number' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'comment'    => '1=Senin, 2=Selasa, ..., 6=Sabtu',
            ],
            'day_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 10,
                'comment'    => 'Senin, Selasa, ..., Sabtu',
            ],
            'period_number' => [
                'type'       => 'TINYINT',
                'constraint' => 2,
                'comment'    => 'Jam ke-1, ke-2, dst',
            ],
            'start_time' => [
                'type'       => 'VARCHAR',
                'constraint' => 10,
                'comment'    => 'Misal: 07:00',
            ],
            'end_time' => [
                'type'       => 'VARCHAR',
                'constraint' => 10,
                'comment'    => 'Misal: 07:40',
            ],

            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('import_id');
        $this->forge->addKey('class_asc_id');
        $this->forge->addKey('teacher_asc_id');
        $this->forge->addKey(['import_id', 'day_number', 'period_number'], false, false, 'idx_schedule_lookup');
        $this->forge->addKey(['import_id', 'teacher_asc_id', 'day_number'], false, false, 'idx_teacher_schedule');
        $this->forge->addKey(['import_id', 'class_asc_id', 'day_number'], false, false, 'idx_class_schedule');

        $this->forge->createTable('asc_schedules');
    }

    public function down()
    {
        $this->forge->dropTable('asc_schedules');
    }
}