<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * BATCH 5 — T-13
 * ptm_additional_duties: Jabatan non-mengajar beserta ekuivalensi jam.
 */
class CreatePtmAdditionalDuties extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id'                 => ['type' => 'BIGINT',  'constraint' => 20,    'unsigned' => true, 'auto_increment' => true],
            'academic_period_id' => ['type' => 'BIGINT',  'constraint' => 20,    'unsigned' => true, 'null' => false],
            'teacher_id'         => ['type' => 'BIGINT',  'constraint' => 20,    'unsigned' => true, 'null' => false],
            'duty_type_id'       => ['type' => 'INT',     'constraint' => 10,    'unsigned' => true, 'null' => false],
            'classroom_id'       => ['type' => 'BIGINT',  'constraint' => 20,    'unsigned' => true, 'null' => true, 'comment' => 'Nullable — wajib jika is_class_specific'],
            'default_hours'      => ['type' => 'DECIMAL', 'constraint' => '4,1', 'null' => false],
            'effective_hours'    => ['type' => 'DECIMAL', 'constraint' => '4,1', 'null' => false],
            'override_reason'    => ['type' => 'TEXT',    'null' => true],
            'status'             => ['type' => 'ENUM', 'constraint' => ['DRAFT','ACTIVE','EXPIRED','CANCELLED'], 'null' => false, 'default' => 'DRAFT'],
            'notes'              => ['type' => 'TEXT',    'null' => true],
            'created_by'         => ['type' => 'BIGINT',  'constraint' => 20, 'unsigned' => true, 'null' => false],
            'activated_by'       => ['type' => 'BIGINT',  'constraint' => 20, 'unsigned' => true, 'null' => true],
            'activated_at'       => ['type' => 'DATETIME', 'null' => true],
            'created_at'         => ['type' => 'DATETIME', 'null' => true],
            'updated_at'         => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['academic_period_id', 'teacher_id', 'duty_type_id']);
        $this->forge->addKey(['teacher_id', 'academic_period_id', 'status']); // WorkloadCalculator
        $this->forge->addKey('duty_type_id');
        $this->forge->addKey('status');

        $this->forge->addForeignKey('academic_period_id', 'academic_periods',           'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('teacher_id',         'teachers',                   'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('duty_type_id',       'ptm_additional_duty_types',  'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('classroom_id',       'classrooms',                 'id', 'SET NULL',  'CASCADE');
        $this->forge->addForeignKey('created_by',         'users',                      'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('activated_by',       'users',                      'id', 'RESTRICT', 'CASCADE');
        $this->forge->createTable('ptm_additional_duties', true);
    }

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