<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('forum_threads', function (Blueprint $table) {
            // drop existing FK if present
            try {
                $table->dropForeign(['category_id']);
            } catch (\Exception $e) {
                // ignore if not exists
            }

            $table->foreign('category_id')->references('id')->on('forum_categories')->cascadeOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('forum_threads', function (Blueprint $table) {
            try {
                $table->dropForeign(['category_id']);
            } catch (\Exception $e) {
            }

            $table->foreign('category_id')->references('id')->on('categories')->cascadeOnDelete();
        });
    }
};
