<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * BATCH 1 — T-04
 * ptm_regulation_packages: Anchor utama konfigurasi PTM per kurikulum/regulasi.
 */
class CreatePtmRegulationPackages extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 10,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'code'           => ['type' => 'VARCHAR', 'constraint' => 50,  'null' => false, 'comment' => 'MERDEKA_MTS, K13_MTS, KMA_347'],
            'name'           => ['type' => 'VARCHAR', 'constraint' => 150, 'null' => false],
            'jenjang'        => ['type' => 'ENUM', 'constraint' => ['RA','MI','MTs','MA','MAK'], 'null' => false, 'default' => 'MTs'],
            'description'    => ['type' => 'TEXT', 'null' => true],
            'is_active'      => ['type' => 'TINYINT', 'constraint' => 1, 'null' => false, 'default' => 1],
            'effective_from' => ['type' => 'DATE', 'null' => true, 'comment' => 'NULL = berlaku sejak dibuat'],
            'created_by'     => ['type' => 'BIGINT', 'constraint' => 20, 'unsigned' => true, 'null' => false],
            'created_at'     => ['type' => 'DATETIME', 'null' => true],
            'updated_at'     => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('code');
        $this->forge->addKey(['jenjang', 'is_active']);
        $this->forge->addKey('is_active');
        $this->forge->addForeignKey('created_by', 'users', 'id', 'RESTRICT', 'CASCADE');
        $this->forge->createTable('ptm_regulation_packages', true);
    }

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