<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateExamProctorsTables extends Migration
{
    public function up()
    {
        // 1. Add proctors_per_room to exam_events
        $fields = [
            'proctors_per_room' => [
                'type'       => 'INT',
                'constraint' => 11,
                'default'    => 1,
                'null'       => false,
                'after'      => 'is_locked',
            ],
        ];
        $this->forge->addColumn('exam_events', $fields);

        // 2. Create exam_event_teachers table
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'exam_event_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => false,
            ],
            'teacher_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => false,
            ],
            'role' => [
                'type'       => 'VARCHAR',
                'constraint' => 20, // 'panitia' or 'pengawas'
                'null'       => false,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['exam_event_id', 'teacher_id']);
        $this->forge->createTable('exam_event_teachers', true);

        // 3. Create exam_schedule_proctors table
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'exam_event_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => false,
            ],
            'exam_schedule_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => false,
            ],
            'subject_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
                'null'       => false,
            ],
            'exam_room_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => false,
            ],
            'teacher_id_1' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'teacher_id_2' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['exam_schedule_id', 'subject_name', 'exam_room_id']);
        $this->forge->createTable('exam_schedule_proctors', true);
    }

    public function down()
    {
        $this->forge->dropTable('exam_schedule_proctors');
        $this->forge->dropTable('exam_event_teachers');
        $this->forge->dropColumn('exam_events', 'proctors_per_room');
    }
}
