<?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('stock_movements', function (Blueprint $table) {
            // Drop the foreign key constraint first
            $table->dropForeign(['branch_id']);
            
            // Make branch_id nullable
            $table->foreignId('branch_id')->nullable()->change();
            
            // Re-add the foreign key constraint with nullable
            $table->foreign('branch_id')->references('id')->on('branches')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('stock_movements', function (Blueprint $table) {
            // Drop the nullable foreign key
            $table->dropForeign(['branch_id']);
            
            // Make branch_id not nullable again
            $table->foreignId('branch_id')->nullable(false)->change();
            
            // Re-add the original foreign key constraint
            $table->foreign('branch_id')->references('id')->on('branches')->cascadeOnDelete();
        });
    }
};