<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * BATCH 2 — T-05
 * ptm_workload_type_overrides: Override faktor ekuivalensi per Paket Regulasi.
 */
class CreatePtmWorkloadTypeOverrides 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],
            'workload_type_id'      => ['type' => 'INT',     'constraint' => 10,    'unsigned' => true, 'null' => false],
            'equivalence_factor'    => ['type' => 'DECIMAL', 'constraint' => '3,2', 'null' => false,    'comment' => '0.00 hingga 1.00'],
            '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', 'workload_type_id']);
        $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_workload_type_overrides', true);
    }

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