<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * BATCH 1 — T-03
 * ptm_additional_duty_types: Katalog jabatan non-mengajar + jam ekuivalensi default.
 */
class CreatePtmAdditionalDutyTypes 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' => 'WAKA_KUR, WALI_KELAS, KA_LAB, KA_PERPUS'],
            'name'                      => ['type' => 'VARCHAR', 'constraint' => 150, 'null' => false],
            'default_equivalence_hours' => ['type' => 'DECIMAL', 'constraint' => '4,1', 'null' => false, 'default' => '0.0', 'comment' => 'Jam ekuivalen per minggu default'],
            'max_per_period'            => ['type' => 'TINYINT', 'constraint' => 3, 'unsigned' => true, 'null' => true, 'default' => null, 'comment' => 'NULL = tidak dibatasi'],
            'is_class_specific'         => ['type' => 'TINYINT', 'constraint' => 1, 'null' => false, 'default' => 0, 'comment' => '1 = terikat ke rombel'],
            'description'               => ['type' => 'TEXT', 'null' => true],
            'is_active'                 => ['type' => 'TINYINT', 'constraint' => 1, 'null' => false, 'default' => 1],
            '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->addKey('is_active');
        $this->forge->createTable('ptm_additional_duty_types', true);

        $this->db->table('ptm_additional_duty_types')->insertBatch([
            ['code' => 'KEPALA_SEKOLAH', 'name' => 'Kepala Sekolah',        'default_equivalence_hours' => 18.0, 'max_per_period' => 1,    'is_class_specific' => 0, 'sort_order' => 1, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['code' => 'WAKA_KUR',       'name' => 'Wakil Kepala Kurikulum', 'default_equivalence_hours' => 12.0, 'max_per_period' => 1,    'is_class_specific' => 0, 'sort_order' => 2, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['code' => 'WAKA_KESISWAAN','name' => 'Wakil Kepala Kesiswaan', 'default_equivalence_hours' => 12.0, 'max_per_period' => 1,    'is_class_specific' => 0, 'sort_order' => 3, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['code' => 'WAKA_SARPRAS',  'name' => 'Wakil Kepala Sarana',   'default_equivalence_hours' => 12.0, 'max_per_period' => 1,    'is_class_specific' => 0, 'sort_order' => 4, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['code' => 'WALI_KELAS',    'name' => 'Wali Kelas',             'default_equivalence_hours' => 2.0,  'max_per_period' => null, 'is_class_specific' => 1, 'sort_order' => 5, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['code' => 'KA_LAB',        'name' => 'Kepala Laboratorium',    'default_equivalence_hours' => 12.0, 'max_per_period' => null, 'is_class_specific' => 0, 'sort_order' => 6, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['code' => 'KA_PERPUS',     'name' => 'Kepala Perpustakaan',    'default_equivalence_hours' => 12.0, 'max_per_period' => 1,    'is_class_specific' => 0, 'sort_order' => 7, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['code' => 'BK',            'name' => 'Guru BK/Konselor',       'default_equivalence_hours' => 2.0,  'max_per_period' => null, 'is_class_specific' => 0, 'sort_order' => 8, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['code' => 'PEMBINA_OSIS',  'name' => 'Pembina OSIS',           'default_equivalence_hours' => 2.0,  'max_per_period' => 1,    'is_class_specific' => 0, 'sort_order' => 9, '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_additional_duty_types', true);
    }
}
