<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * BATCH 3 — T-10
 * ptm_teacher_competencies: Daftar mapel yang bisa diajarkan guru beserta prioritas.
 */
class CreatePtmTeacherCompetencies extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id'                => ['type' => 'INT',    'constraint' => 10, 'unsigned' => true, 'auto_increment' => true],
            'teacher_id'        => ['type' => 'BIGINT', 'constraint' => 20, 'unsigned' => true, 'null' => false],
            'school_subject_id' => ['type' => 'BIGINT', 'constraint' => 20, 'unsigned' => true, 'null' => false],
            'priority_order'    => ['type' => 'TINYINT', 'constraint' => 3, 'unsigned' => true, 'null' => false, 'default' => 1, 'comment' => '1 = utama, 2 = pendukung'],
            'created_at'        => ['type' => 'DATETIME', 'null' => true],
            'updated_at'        => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['teacher_id', 'school_subject_id']);
        $this->forge->addKey('teacher_id');
        $this->forge->addKey('school_subject_id');
        $this->forge->addKey(['teacher_id', 'priority_order']);
        $this->forge->addForeignKey('teacher_id',        'teachers',        'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('school_subject_id', 'school_subjects', 'id', 'RESTRICT', 'CASCADE');
        $this->forge->createTable('ptm_teacher_competencies', true);
    }

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