<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class DropTeacherAssignmentsTable extends Migration
{
    public function up()
    {
        // teacher_assignments sudah digantikan sepenuhnya oleh:
        // - teaching_assignments (untuk guru mapel)
        // - bk_assignments (untuk guru BK)
        if ($this->db->tableExists('teacher_assignments')) {
            $this->forge->dropTable('teacher_assignments', true);
        }
    }

    public function down()
    {
        $this->forge->addField([
            'id'                 => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'teacher_id'         => ['type' => 'BIGINT', 'unsigned' => true],
            'classroom_id'       => ['type' => 'BIGINT', 'unsigned' => true],
            'subject_id'         => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'academic_period_id' => ['type' => 'BIGINT', 'unsigned' => true],
            'assignment_type'    => [
                'type'       => 'ENUM',
                'constraint' => ['bk', 'subject_teacher'],
                'default'    => 'subject_teacher',
            ],
            '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('subject_id', 'subjects', 'id', 'SET NULL', 'CASCADE');
        $this->forge->addForeignKey('academic_period_id', 'academic_periods', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addUniqueKey(['teacher_id', 'classroom_id', 'subject_id', 'academic_period_id']);
        $this->forge->createTable('teacher_assignments');
    }
}
