<?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('vehicles', function (Blueprint $table) {
            // Drop the foreign key constraint first
            $table->dropForeign(['operator_id']);
            
            // Modify the column to be nullable
            $table->unsignedBigInteger('operator_id')->nullable()->change();
            
            // Add the foreign key constraint back with nullOnDelete
            $table->foreign('operator_id')->references('id')->on('operators')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('vehicles', function (Blueprint $table) {
            $table->dropForeign(['operator_id']);
            $table->unsignedBigInteger('operator_id')->change();
            $table->foreign('operator_id')->references('id')->on('operators')->onDelete('cascade');
        });
    }
};
