<?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('kot_items', function (Blueprint $table) {
            if (! Schema::hasColumn('kot_items', 'item_id')) {
                $table->foreignId('item_id')->nullable()->constrained()->nullOnDelete();
            }
            if (! Schema::hasColumn('kot_items', 'deal_id')) {
                $table->foreignId('deal_id')->nullable()->constrained()->nullOnDelete();
            }
        });
    }

    public function down(): void
    {
        Schema::table('kot_items', function (Blueprint $table) {
            if (Schema::hasColumn('kot_items', 'item_id')) {
                $table->dropForeign(['item_id']);
                $table->dropColumn('item_id');
            }
            if (Schema::hasColumn('kot_items', 'deal_id')) {
                $table->dropForeign(['deal_id']);
                $table->dropColumn('deal_id');
            }
        });
    }
};
