<?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('chat_rooms', function (Blueprint $table) {
            // Sütun zaten varsa sadece foreign key ekle
            if (!Schema::hasColumn('chat_rooms', 'category_id')) {
                $table->foreignId('category_id')
                      ->nullable()
                      ->after('description')
                      ->constrained('chat_categories')
                      ->onDelete('set null');
            } else {
                // Sadece foreign key constraint ekle
                $table->foreign('category_id')
                      ->references('id')
                      ->on('chat_categories')
                      ->onDelete('set null');
            }
        });
    }

    public function down(): void
    {
        Schema::table('chat_rooms', function (Blueprint $table) {
            $table->dropForeign(['category_id']);
        });
    }
};
