<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class DropUnusedTables extends Migration
{
    public function up()
    {
        // ── Nomor 2: Drop tabel assessment orphan ────────────────────────
        // assessment_settings & assessment_essay_details sudah digantikan oleh
        // assessment_class_settings & assessment_detail_scores

        if ($this->db->tableExists('assessment_essay_details')) {
            $this->forge->dropTable('assessment_essay_details', true);
        }

        if ($this->db->tableExists('assessment_settings')) {
            $this->forge->dropTable('assessment_settings', true);
        }

        // ── Nomor 4: Drop tabel teacher zombie ───────────────────────────
        // teacher_subjects & teacher_classes sudah digantikan sepenuhnya
        // oleh teaching_assignments yang lebih lengkap (ada periode, unique key, dll)

        if ($this->db->tableExists('teacher_classes')) {
            $this->forge->dropTable('teacher_classes', true);
        }

        if ($this->db->tableExists('teacher_subjects')) {
            $this->forge->dropTable('teacher_subjects', true);
        }
    }

    public function down()
    {
        // ── Restore assessment_settings ───────────────────────────────────
        $this->forge->addField([
            'id'                     => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'assessment_id'          => ['type' => 'BIGINT', 'unsigned' => true],
            'kkm'                    => ['type' => 'TINYINT', 'default' => 75],
            'weight_multiple_choice' => ['type' => 'TINYINT', 'default' => 100],
            'weight_essay'           => ['type' => 'TINYINT', 'default' => 0],
            'answer_key'             => ['type' => 'TEXT', 'null' => true],
            'total_questions'        => ['type' => 'SMALLINT', 'default' => 0],
            'essay_mode'             => ['type' => 'TINYINT', 'default' => 0],
            'total_essay_questions'  => ['type' => 'SMALLINT', 'default' => 0],
            'created_at'             => ['type' => 'DATETIME', 'null' => true],
            'updated_at'             => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addForeignKey('assessment_id', 'assessments', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('assessment_settings');

        // ── Restore assessment_essay_details ─────────────────────────────
        $this->forge->addField([
            'id'              => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'score_id'        => ['type' => 'BIGINT', 'unsigned' => true],
            'question_number' => ['type' => 'SMALLINT'],
            'score'           => ['type' => 'DECIMAL', 'constraint' => '5,2', 'null' => true],
            'created_at'      => ['type' => 'DATETIME', 'null' => true],
            'updated_at'      => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addForeignKey('score_id', 'assessment_scores', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('assessment_essay_details');

        // ── Restore teacher_subjects ──────────────────────────────────────
        $this->forge->addField([
            'id'                 => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'teacher_id'         => ['type' => 'BIGINT', 'unsigned' => true],
            'subject_id'         => ['type' => 'BIGINT', 'unsigned' => true],
            'academic_period_id' => ['type' => 'BIGINT', 'unsigned' => true],
            'created_at'         => ['type' => 'DATETIME', 'null' => true],
            'updated_at'         => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addForeignKey('teacher_id', 'teachers', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('subject_id', 'subjects', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('academic_period_id', 'academic_periods', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('teacher_subjects');

        // ── Restore teacher_classes ───────────────────────────────────────
        $this->forge->addField([
            'id'                 => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'teacher_id'         => ['type' => 'BIGINT', 'unsigned' => true],
            'subject_id'         => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'classroom_id'       => ['type' => 'BIGINT', 'unsigned' => true],
            'academic_period_id' => ['type' => 'BIGINT', 'unsigned' => true],
            'created_at'         => ['type' => 'DATETIME', 'null' => true],
            'updated_at'         => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addForeignKey('teacher_id', 'teachers', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('subject_id', 'subjects', 'id', 'SET NULL', 'CASCADE');
        $this->forge->addForeignKey('classroom_id', 'classrooms', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('academic_period_id', 'academic_periods', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('teacher_classes');
    }
}
