<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateLetterLogsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'letter_type' => [
                'type'       => 'ENUM',
                'constraint' => ['keterangan_aktif', 'mutasi_keluar'],
                'null'       => false,
            ],
            'letter_number' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
                'null'       => false,
            ],
            'student_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => false,
            ],
            'student_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
                'null'       => false,
            ],
            'student_nis' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'null'       => false,
            ],
            'letter_date' => [
                'type' => 'DATE',
                'null' => false,
            ],
            'purpose' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'destination' => [
                'type'       => 'VARCHAR',
                'constraint' => 200,
                'null'       => true,
            ],
            'reason' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'printed_by' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => false,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        // Remove foreign key constraints temporarily to avoid data type mismatch issues
        // $this->forge->addForeignKey('student_id', 'students', 'id', 'CASCADE', 'CASCADE');
        // $this->forge->addForeignKey('printed_by', 'users', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('letter_logs');
    }

    public function down()
    {
        $this->forge->dropTable('letter_logs');
    }
}
