<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateScheduleTimeslots extends Migration
{
    public function up()
    {
        // Tabel konfigurasi slot waktu
        $this->forge->addField([
            'id'           => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'day'          => ['type' => 'TINYINT', 'unsigned' => true, 'comment' => '1=Senin,2=Selasa,...,6=Sabtu'],
            'slot_number'  => ['type' => 'TINYINT', 'unsigned' => true, 'comment' => 'Urutan jam ke-'],
            'label'        => ['type' => 'VARCHAR', 'constraint' => 30, 'comment' => 'misal: Jam 1, Istirahat'],
            'start_time'   => ['type' => 'TIME'],
            'end_time'     => ['type' => 'TIME'],
            'is_break'     => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0, 'comment' => '1=istirahat'],
            'is_active'    => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 1],
            'created_at'   => ['type' => 'DATETIME', 'null' => true],
            'updated_at'   => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey(['day', 'slot_number']);
        $this->forge->createTable('schedule_timeslots', true);

        // Tabel header jadwal (satu jadwal per tahun ajaran + periode)
        $this->forge->addField([
            'id'                  => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'academic_year_id'    => ['type' => 'INT', 'unsigned' => true],
            'academic_period_id'  => ['type' => 'INT', 'unsigned' => true],
            'name'                => ['type' => 'VARCHAR', 'constraint' => 100],
            'active_days'         => ['type' => 'VARCHAR', 'constraint' => 20, 'default' => '1,2,3,4,5',
                                      'comment' => 'CSV hari aktif: 1=Senin dst'],
            'status'              => ['type' => "ENUM('draft','published')", 'default' => 'draft'],
            'notes'               => ['type' => 'TEXT', 'null' => true],
            'created_at'          => ['type' => 'DATETIME', 'null' => true],
            'updated_at'          => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey(['academic_year_id', 'academic_period_id']);
        $this->forge->createTable('schedules', true);
    }

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