<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class FinalMigrationFix extends Migration
{
    public function up()
    {
        $db = db_connect();

        // ── HAPUS SEMUA RECORD MIGRASI YANG BERMASALAH ─────
        $db->query("DELETE FROM migrations WHERE version >= '2026-06-22-124000'");

        // ── TAMBAH ULANG RECORD AGAR TIDAK DIJALANKAN LAGI ──
        $batch = (int) $db->query("SELECT MAX(batch) AS b FROM migrations")->getRow()->b + 1;

        // Daftar migration yang sudah fix — kita catat ulang
        $migrations = [
            ['App\\Database\\Migrations\\AddAiSettingsToSchoolSettings', '2026-06-22-124000'],
            ['App\\Database\\Migrations\\AlterStudentsForGraduation', '2026-06-25-000007'],
            ['App\\Database\\Migrations\\CreateGraduationSettingsTable', '2026-06-25-000008'],
            ['App\\Database\\Migrations\\CreateExamTables', '2026-06-25-000001'],
            ['App\\Database\\Migrations\\AddPairingPlanToExamEvents', '2026-06-26-000001'],
            ['App\\Database\\Migrations\\AddEventCodeToExamEvents', '2026-06-26-000002'],
            ['App\\Database\\Migrations\\AddRulesToSchoolSettings', '2026-06-26-000003'],
            ['App\\Database\\Migrations\\AddRulesAndChairmanToExamEvents', '2026-06-26-000004'],
            ['App\\Database\\Migrations\\CreateExamProctorsTables', '2026-06-26-000005'],
            ['App\\Database\\Migrations\\AddMaxDaysToExamEventTeachers', '2026-06-26-000006'],
            ['App\\Database\\Migrations\\AlterLetterLogsForTeacher', '2026-06-26-000010'],
            ['App\\Database\\Migrations\\CreateLetterTeachersTable', '2026-06-26-000011'],
            ['App\\Database\\Migrations\\AddGolonganToTeachers', '2026-06-26-000012'],
            ['App\\Database\\Migrations\\AddLetterOriginToSchoolSettings', '2026-06-26-000013'],
            ['App\\Database\\Migrations\\CreateLearningDevicesTables', '2026-06-28-000001'],
            ['App\\Database\\Migrations\\AddSemesterToTps', '2026-06-28-000002'],
            ['App\\Database\\Migrations\\CreateLearningDeviceKktpTable', '2026-06-28-000003'],
            ['App\\Database\\Migrations\\CreateLearningDeviceRpeAndPekanEfektifTables', '2026-06-28-000004'],
            ['App\\Database\\Migrations\\AlterAiKbCpTable', '2026-06-28-000005'],
            ['App\\Database\\Migrations\\AddCpElementsToLearningDevices', '2026-06-28-000006'],
            ['App\\Database\\Migrations\\AddModulFieldColumnsToLearningDeviceModuls', '2026-06-29-000001'],
            ['App\\Database\\Migrations\\ChapterFieldForTpAndModul', '2026-06-30-000001'],
            ['App\\Database\\Migrations\\MakeTpIdNullableInModuls', '2026-07-01-000001'],
            ['App\\Database\\Migrations\\AddChapterCpMapToLearningDevices', '2026-07-02-000001'],
            ['App\\Database\\Migrations\\AddChapterDplKbcToLearningDevices', '2026-07-03-000001'],
            ['App\\Database\\Migrations\\AddMissingLearningDeviceColumns', '2026-07-04-000001'],
            // CreateAiKbTpTable sengaja tidak dicatat — tabel ai_kb_tp belum ada di online
            // CreateStudentGraduationsTable sengaja tidak dicatat — tabel student_graduations belum ada di online
            ['App\\Database\\Migrations\\AddAllowSklDownloadToGraduationSettings', '2026-07-04-000004'],
            ['App\\Database\\Migrations\\ReaddTpIdForeignKeyToLearningDeviceModuls', '2026-07-04-000005'],
            ['App\\Database\\Migrations\\EnsureGraduationTablesExist', '2026-07-04-000006'],
            ['App\\Database\\Migrations\\CreateTeachingJournalsTable', '2026-07-05-000001'],
            ['App\\Database\\Migrations\\EnsureTeachingJournalsTable', '2026-07-05-000002'],
            ['App\\Database\\Migrations\\FixMigrationErrors', '2026-07-05-000003'],
        ];

        $now = time();
        foreach ($migrations as $m) {
            $db->query("INSERT IGNORE INTO migrations (version, class, `group`, namespace, time, batch) VALUES (?, ?, 'default', 'App', ?, ?)",
                [$m[1], $m[0], $now, $batch]);
        }
    }

    public function down()
    {
        // One-way fix
    }
}
