<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AlterLetterLogsForTeacher extends Migration
{
    public function up()
    {
        if ($this->db->fieldExists('teacher_id', 'letter_logs')) return;
        // 1. Ubah letter_type ENUM untuk menambah nilai baru
        $this->db->query("ALTER TABLE letter_logs
            MODIFY COLUMN letter_type ENUM(
                'keterangan_aktif',
                'mutasi_keluar',
                'surat_tugas',
                'sppd'
            ) NOT NULL");

        // 2. Buat student_id, student_name, student_nis nullable
        //    (karena surat tugas/SPPD tidak punya siswa)
        $this->db->query("ALTER TABLE letter_logs
            MODIFY COLUMN student_id   INT(11) UNSIGNED NULL DEFAULT NULL,
            MODIFY COLUMN student_name VARCHAR(100)     NULL DEFAULT NULL,
            MODIFY COLUMN student_nis  VARCHAR(20)      NULL DEFAULT NULL");

        // 3. Tambah kolom-kolom baru untuk surat tugas & SPPD
        $this->forge->addColumn('letter_logs', [

            // --- Surat Tugas & SPPD ---
            'task_description' => [
                'type' => 'TEXT',
                'null' => true,
                'after' => 'reason',
                'comment' => 'Isi/uraian tugas (surat_tugas)',
            ],

            // --- SPPD ---
            'origin' => [
                'type'       => 'VARCHAR',
                'constraint' => 200,
                'null'       => true,
                'after'      => 'task_description',
                'comment'    => 'Tempat berangkat (SPPD)',
            ],
            'transport' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
                'null'       => true,
                'after'      => 'origin',
                'comment'    => 'Alat angkut (SPPD)',
            ],
            'date_start' => [
                'type' => 'DATE',
                'null' => true,
                'after' => 'transport',
                'comment' => 'Tanggal berangkat (SPPD)',
            ],
            'date_end' => [
                'type' => 'DATE',
                'null' => true,
                'after' => 'date_start',
                'comment' => 'Tanggal kembali (SPPD)',
            ],
            'budget_source' => [
                'type'       => 'VARCHAR',
                'constraint' => 200,
                'null'       => true,
                'after'      => 'date_end',
                'comment'    => 'Pembebanan anggaran (SPPD)',
            ],
        ]);
    }

    public function down()
    {
        $this->forge->dropColumn('letter_logs', [
            'task_description',
            'origin',
            'transport',
            'date_start',
            'date_end',
            'budget_source',
        ]);

        $this->db->query("ALTER TABLE letter_logs
            MODIFY COLUMN student_id   INT(11) UNSIGNED NOT NULL,
            MODIFY COLUMN student_name VARCHAR(100)     NOT NULL,
            MODIFY COLUMN student_nis  VARCHAR(20)      NOT NULL");

        $this->db->query("ALTER TABLE letter_logs
            MODIFY COLUMN letter_type ENUM('keterangan_aktif','mutasi_keluar') NOT NULL");
    }
}