<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    public function up(): void
    {
        // === SISWAS — id_kelas varchar → bigint ===
        Schema::table('siswas', function (Blueprint $table) {
            $table->unsignedBigInteger('id_kelas_baru')->nullable()->after('id_kelas');
        });
        DB::statement("UPDATE siswas s JOIN kelas k ON s.id_kelas = k.id_kelas_old SET s.id_kelas_baru = k.id_kelas");
        Schema::table('siswas', function (Blueprint $table) {
            $table->unsignedBigInteger('id_kelas_baru')->nullable(false)->change();
            $table->foreign('id_kelas_baru', 'siswas_id_kelas_new_foreign')->references('id_kelas')->on('kelas')->onDelete('cascade');
        });
        Schema::table('siswas', function (Blueprint $table) {
            $table->dropColumn('id_kelas');
        });
        DB::statement('ALTER TABLE siswas CHANGE id_kelas_baru id_kelas BIGINT UNSIGNED NOT NULL');

        // === MENGAJARS — id_kelas varchar → bigint ===
        Schema::table('mengajars', function (Blueprint $table) {
            $table->unsignedBigInteger('id_kelas_baru')->nullable()->after('id_kelas');
        });
        DB::statement("UPDATE mengajars m JOIN kelas k ON m.id_kelas = k.id_kelas_old SET m.id_kelas_baru = k.id_kelas");
        Schema::table('mengajars', function (Blueprint $table) {
            $table->unsignedBigInteger('id_kelas_baru')->nullable(false)->change();
            $table->foreign('id_kelas_baru', 'mengajars_id_kelas_new_foreign')->references('id_kelas')->on('kelas')->onDelete('cascade');
        });
        Schema::table('mengajars', function (Blueprint $table) {
            $table->dropColumn('id_kelas');
        });
        DB::statement('ALTER TABLE mengajars CHANGE id_kelas_baru id_kelas BIGINT UNSIGNED NOT NULL');

        // === LEADERBOARDS — id_kelas varchar → bigint ===
        Schema::table('leaderboards', function (Blueprint $table) {
            $table->unsignedBigInteger('id_kelas_baru')->nullable()->after('id_kelas');
        });
        DB::statement("UPDATE leaderboards l JOIN kelas k ON l.id_kelas = k.id_kelas_old SET l.id_kelas_baru = k.id_kelas");
        Schema::table('leaderboards', function (Blueprint $table) {
            $table->unsignedBigInteger('id_kelas_baru')->nullable(false)->change();
            $table->foreign('id_kelas_baru', 'leaderboards_id_kelas_new_foreign')->references('id_kelas')->on('kelas')->onDelete('cascade');
        });
        Schema::table('leaderboards', function (Blueprint $table) {
            $table->dropColumn('id_kelas');
        });
        DB::statement('ALTER TABLE leaderboards CHANGE id_kelas_baru id_kelas BIGINT UNSIGNED NOT NULL');

        // === Hapus kolom id_kelas_old dari kelas ===
        Schema::table('kelas', function (Blueprint $table) {
            $table->dropColumn('id_kelas_old');
        });
    }

    public function down(): void
    {
        throw new \Exception('Rollback tidak didukung. Restore dari backup.');
    }
};