<?php

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

return new class extends Migration {
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Drop FK on jadwal_praktik if present, then modify column types to BIGINT and re-add FK
        try {
            DB::statement('ALTER TABLE `jadwal_praktik` DROP FOREIGN KEY `jadwal_praktik_ibfk_1`;');
        } catch (\Throwable $e) {
            // ignore if it doesn't exist
        }

        // Modify types to BIGINT which can hold long NIP values
        DB::statement('ALTER TABLE `guru` MODIFY `nip` BIGINT NOT NULL;');
        DB::statement('ALTER TABLE `jadwal_praktik` MODIFY `nip_guru` BIGINT NOT NULL;');

        // Recreate FK
        DB::statement('ALTER TABLE `jadwal_praktik` ADD CONSTRAINT `jadwal_praktik_ibfk_1` FOREIGN KEY (`nip_guru`) REFERENCES `guru`(`nip`);');
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        try {
            DB::statement('ALTER TABLE `jadwal_praktik` DROP FOREIGN KEY `jadwal_praktik_ibfk_1`;');
        } catch (\Throwable $e) {
        }

        // Revert back to INT (note: this may fail if values exceed INT range)
        DB::statement('ALTER TABLE `jadwal_praktik` MODIFY `nip_guru` INT NOT NULL;');
        DB::statement('ALTER TABLE `guru` MODIFY `nip` INT NOT NULL;');

        DB::statement('ALTER TABLE `jadwal_praktik` ADD CONSTRAINT `jadwal_praktik_ibfk_1` FOREIGN KEY (`nip_guru`) REFERENCES `guru`(`nip`);');
    }
};
