<?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('estimates', function (Blueprint $table) {
            // Add customer and vehicle foreign keys for standalone estimates
            $table->foreignId('customer_id')->nullable()->constrained()->onDelete('cascade');
            $table->foreignId('vehicle_id')->nullable()->constrained()->onDelete('cascade');

            // Make job_card_id and diagnosis_id nullable for standalone estimates
            $table->foreignId('job_card_id')->nullable()->change();
            $table->foreignId('diagnosis_id')->nullable()->change();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('estimates', function (Blueprint $table) {
            $table->dropForeign(['customer_id']);
            $table->dropForeign(['vehicle_id']);
            $table->dropColumn(['customer_id', 'vehicle_id']);

            // Restore job_card_id and diagnosis_id as required
            $table->foreignId('job_card_id')->nullable(false)->change();
            $table->foreignId('diagnosis_id')->nullable(false)->change();
        });
    }
};
