<?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('wine_productions', function (Blueprint $table) {
            // Drop the existing foreign key constraint
            $table->dropForeign(['wine_id']);

            // Make wine_id nullable
            $table->foreignId('wine_id')->nullable()->change();

            // Re-add the foreign key with onDelete('set null')
            $table->foreign('wine_id')
                  ->references('id')
                  ->on('wines')
                  ->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('wine_productions', function (Blueprint $table) {
            // Drop the modified foreign key
            $table->dropForeign(['wine_id']);

            // Make wine_id non-nullable again
            $table->foreignId('wine_id')->nullable(false)->change();

            // Re-add the original foreign key with cascade
            $table->foreign('wine_id')
                  ->references('id')
                  ->on('wines')
                  ->onDelete('cascade');
        });
    }
};
