<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateBkAssignmentsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id'                 => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'teacher_id'         => ['type' => 'BIGINT', 'unsigned' => true],
            'classroom_id'       => ['type' => 'BIGINT', 'unsigned' => true],
            'academic_period_id' => ['type' => 'BIGINT', 'unsigned' => true],
            'created_at'         => ['type' => 'DATETIME', 'null' => true],
            'updated_at'         => ['type' => 'DATETIME', 'null' => true],
        ]);
        
        $this->forge->addPrimaryKey('id');
        $this->forge->addForeignKey('teacher_id', 'teachers', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('classroom_id', 'classrooms', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('academic_period_id', 'academic_periods', 'id', 'CASCADE', 'CASCADE');
        
        // Unique: satu kelas tidak boleh punya lebih dari satu BK di periode yang sama
        $this->forge->addUniqueKey(['classroom_id', 'academic_period_id']);
        
        $this->forge->createTable('bk_assignments');
    }

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