<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('mahasiswa', function (Blueprint $table) {
            // Tambahkan kolom rap_id
            $table->foreignId('rap_id')
                ->nullable()
                ->after('prodi_id')
                ->constrained('rap')
                ->nullOnDelete();

            // Tambahkan index untuk performa query
            $table->index('rap_id');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('mahasiswa', function (Blueprint $table) {
            // Hapus foreign key terlebih dahulu
            $table->dropForeign(['rap_id']);

            // Hapus index
            $table->dropIndex(['rap_id']);

            // Hapus kolom
            $table->dropColumn('rap_id');
        });
    }
};
