<?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('accounts', function (Blueprint $table) {
            // Drop the old foreign key constraint that references 'shops' table
            $table->dropForeign(['shop_id']);
            
            // Create a new foreign key constraint that references 'accounts' table
            // This allows shop_id to reference accounts where model_type='shop'
            $table->foreign('shop_id')->references('id')->on('accounts')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('accounts', function (Blueprint $table) {
            // Drop the new foreign key constraint
            $table->dropForeign(['shop_id']);
            
            // Restore the old foreign key constraint that references 'shops' table
            $table->foreign('shop_id')->references('id')->on('shops')->onDelete('set null');
        });
    }
};
