<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateLetterTeachersTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'letter_log_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => false,
                'comment'    => 'FK ke letter_logs.id',
            ],
            'teacher_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => true,
                'comment'    => 'FK ke teachers.id; NULL jika guru tidak ada di master',
            ],
            'teacher_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
                'null'       => false,
                'comment'    => 'Nama snapshot saat surat dibuat',
            ],
            'teacher_nip' => [
                'type'       => 'VARCHAR',
                'constraint' => 30,
                'null'       => true,
            ],
            'teacher_golongan' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'null'       => true,
                'comment'    => 'Pangkat/Gol saat surat dibuat',
            ],
            'teacher_jabatan' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
                'null'       => true,
                'comment'    => 'Jabatan diisi manual di form',
            ],
            'sort_order' => [
                'type'       => 'TINYINT',
                'constraint' => 3,
                'unsigned'   => true,
                'default'    => 1,
                'comment'    => 'Urutan guru dalam surat (untuk multi-guru)',
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('letter_log_id');
        $this->forge->createTable('letter_teachers');
    }

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