<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * BATCH 6 — T-15
 * ptm_sk_details: Snapshot immutable beban kerja per guru saat SK diterbitkan.
 */
class CreatePtmSkDetails extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id'                     => ['type' => 'BIGINT',  'constraint' => 20,    'unsigned' => true, 'auto_increment' => true],
            'sk_document_id'         => ['type' => 'BIGINT',  'constraint' => 20,    'unsigned' => true, 'null' => false],
            'teacher_id'             => ['type' => 'BIGINT',  'constraint' => 20,    'unsigned' => true, 'null' => false],
            'teaching_hours'         => ['type' => 'DECIMAL', 'constraint' => '5,1', 'null' => false, 'default' => '0.0'],
            'cocurricular_hours'     => ['type' => 'DECIMAL', 'constraint' => '5,1', 'null' => false, 'default' => '0.0'],
            'extracurricular_hours'  => ['type' => 'DECIMAL', 'constraint' => '5,1', 'null' => false, 'default' => '0.0'],
            'additional_hours'       => ['type' => 'DECIMAL', 'constraint' => '5,1', 'null' => false, 'default' => '0.0'],
            'total_equivalent_hours' => ['type' => 'DECIMAL', 'constraint' => '5,2', 'null' => false, 'default' => '0.00'],
            'duty_summary'           => ['type' => 'VARCHAR', 'constraint' => 500,   'null' => true, 'comment' => 'Ringkasan jabatan: Wali Kelas 7A, Pembina OSIS'],
            'created_at'             => ['type' => 'DATETIME', 'null' => true, 'comment' => 'Tidak ada updated_at — immutable'],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['sk_document_id', 'teacher_id']);
        $this->forge->addKey('sk_document_id');
        $this->forge->addKey('teacher_id');

        $this->forge->addForeignKey('sk_document_id', 'ptm_sk_documents', 'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('teacher_id',     'teachers',          'id', 'RESTRICT', 'CASCADE');
        $this->forge->createTable('ptm_sk_details', true);
    }

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