<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * BATCH 2 — T-06
 * ptm_program_workload_mappings: Pemetaan program_type ke workload_type per Paket Regulasi.
 */
class CreatePtmProgramWorkloadMappings extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id'                    => ['type' => 'INT',     'constraint' => 10, 'unsigned' => true, 'auto_increment' => true],
            'regulation_package_id' => ['type' => 'INT',     'constraint' => 10, 'unsigned' => true, 'null' => false],
            'program_type'          => ['type' => 'ENUM', 'constraint' => ['REGULAR','P5','P2RA','LITERACY','EXTRACURRICULAR'], 'null' => false],
            'workload_type_id'      => ['type' => 'INT',     'constraint' => 10, 'unsigned' => true, 'null' => false],
            'notes'                 => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
            'created_at'            => ['type' => 'DATETIME', 'null' => true],
            'updated_at'            => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['regulation_package_id', 'program_type']);
        $this->forge->addKey('regulation_package_id');
        $this->forge->addForeignKey('regulation_package_id', 'ptm_regulation_packages', 'id', 'CASCADE',  'CASCADE');
        $this->forge->addForeignKey('workload_type_id',      'ptm_workload_types',       'id', 'RESTRICT', 'CASCADE');
        $this->forge->createTable('ptm_program_workload_mappings', true);
    }

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