<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * BATCH 5 — T-12
 * ptm_assignments: Tabel operasional UTAMA — penugasan formal guru.
 * Lifecycle: DRAFT → LOCKED → REVISED → ARCHIVED
 */
class CreatePtmAssignments 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],
            'blueprint_id'       => ['type' => 'INT',    'constraint' => 10, 'unsigned' => true, 'null' => false],
            'classroom_id'       => ['type' => 'BIGINT', 'constraint' => 20, 'unsigned' => true, 'null' => false],
            'standard_hours'     => ['type' => 'TINYINT', 'constraint' => 3, 'unsigned' => true, 'null' => false, 'comment' => 'Disalin dari blueprint.hours_per_week'],
            'actual_hours'       => ['type' => 'TINYINT', 'constraint' => 3, 'unsigned' => true, 'null' => false],
            'override_reason'    => ['type' => 'TEXT',   'null' => true, 'comment' => 'Wajib jika actual != standard'],
            'status'             => ['type' => 'ENUM', 'constraint' => ['DRAFT','LOCKED','REVISED','ARCHIVED'], 'null' => false, 'default' => 'DRAFT'],
            'revision_of'        => ['type' => 'BIGINT', 'constraint' => 20, 'unsigned' => true, 'null' => true],
            'revision_number'    => ['type' => 'TINYINT', 'constraint' => 3, 'unsigned' => true, 'null' => false, 'default' => 1],
            'notes'              => ['type' => 'TEXT',   'null' => true],
            'created_by'         => ['type' => 'BIGINT', 'constraint' => 20, 'unsigned' => true, 'null' => false],
            'locked_by'          => ['type' => 'BIGINT', 'constraint' => 20, 'unsigned' => true, 'null' => true],
            'locked_at'          => ['type' => 'DATETIME', 'null' => true],
            'created_at'         => ['type' => 'DATETIME', 'null' => true],
            'updated_at'         => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey(['academic_period_id', 'status']);
        $this->forge->addKey(['teacher_id', 'academic_period_id', 'status']); // QUERY TERPANAS
        $this->forge->addKey(['classroom_id', 'academic_period_id']);
        $this->forge->addKey('blueprint_id');
        $this->forge->addKey('revision_of');
        $this->forge->addKey(['academic_period_id', 'teacher_id', 'blueprint_id', 'classroom_id']); // duplicate check

        $this->forge->addForeignKey('academic_period_id', 'academic_periods',         'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('teacher_id',         'teachers',                  'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('blueprint_id',       'ptm_curriculum_blueprints', 'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('classroom_id',       'classrooms',                'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('created_by',         'users',                     'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('locked_by',          'users',                     'id', 'RESTRICT', 'CASCADE');
        $this->forge->createTable('ptm_assignments', true);

        // Self-reference FK
        $this->db->query('ALTER TABLE `ptm_assignments`
            ADD CONSTRAINT `fk_ptm_asgn_revision` FOREIGN KEY (`revision_of`) REFERENCES `ptm_assignments` (`id`) ON DELETE SET NULL ON UPDATE CASCADE');
    }

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