<?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('Hukum', function (Blueprint $table) {
            // Remove the StatusPenaatan column
            $table->dropColumn('StatusPenaatan');

            // Add PenaatanId column as foreign key
            $table->unsignedInteger('PenaatanId')->nullable()->comment('id dari tabel Penaatan untuk mengambil NamaPenaatan');
            $table->foreign('PenaatanId')->references('PenaatanId')->on('Penaatan')->onDelete('set null');

            // Add index for the new column
            $table->index('PenaatanId');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('Hukum', function (Blueprint $table) {
            // Remove the foreign key constraint and the PenaatanId column
            $table->dropForeign(['PenaatanId']);
            $table->dropIndex(['PenaatanId']);
            $table->dropColumn('PenaatanId');

            // Add back the StatusPenaatan column with its original properties
            $table->unsignedTinyInteger('StatusPenaatan')->default(1)->comment('1=pengawasan, 2=peningkatan sanksi, 3=taat');
        });
    }
};
