<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * BATCH 1 — T-01
 * ptm_workload_types: Katalog global jenis beban kerja + faktor ekuivalensi default.
 */
class CreatePtmWorkloadTypes extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 10,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'code' => [
                'type'       => 'VARCHAR',
                'constraint' => 30,
                'null'       => false,
                'comment'    => 'TEACHING, COCURRICULAR, EXTRACURRICULAR, ADDITIONAL_DUTY',
            ],
            'name' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
                'null'       => false,
                'comment'    => 'Mengajar, Kokurikuler, dst',
            ],
            'default_equivalence_factor' => [
                'type'       => 'DECIMAL',
                'constraint' => '3,2',
                'null'       => false,
                'default'    => '1.00',
                'comment'    => '0.00 hingga 1.00',
            ],
            'is_counted_in_total' => [
                'type'    => 'TINYINT',
                'constraint' => 1,
                'null'    => false,
                'default' => 1,
                'comment' => '1 = masuk hitungan total JTM ekuivalen',
            ],
            'description' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'sort_order' => [
                'type'       => 'TINYINT',
                'constraint' => 3,
                'unsigned'   => true,
                'null'       => false,
                'default'    => 0,
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('code');
        $this->forge->createTable('ptm_workload_types', true);

        // Seed data
        $this->db->table('ptm_workload_types')->insertBatch([
            ['code' => 'TEACHING',        'name' => 'Mengajar',        'default_equivalence_factor' => 1.00, 'sort_order' => 1, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['code' => 'COCURRICULAR',    'name' => 'Kokurikuler',     'default_equivalence_factor' => 1.00, 'sort_order' => 2, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['code' => 'EXTRACURRICULAR', 'name' => 'Ekstrakurikuler', 'default_equivalence_factor' => 0.50, 'sort_order' => 3, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['code' => 'ADDITIONAL_DUTY', 'name' => 'Tugas Tambahan',  'default_equivalence_factor' => 1.00, 'sort_order' => 4, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
        ]);
    }

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