<?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::create('job_cards', function (Blueprint $table) {
            $table->id();
            $table->string('job_card_number')->unique();
            $table->string('branch_code', 10)->default('ACC');
            $table->foreignId('customer_id')->constrained()->onDelete('cascade');
            $table->foreignId('vehicle_id')->constrained()->onDelete('cascade');
            $table->foreignId('service_advisor_id')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamp('arrival_datetime');
            $table->timestamp('expected_completion_date')->nullable();
            $table->integer('mileage_in')->nullable();
            $table->integer('mileage_out')->nullable();
            $table->string('fuel_level_in', 20)->nullable();
            $table->string('fuel_level_out', 20)->nullable();
            $table->enum('status', ['received', 'in_diagnosis', 'estimate_sent', 'approved', 'in_progress', 'quality_check', 'completed', 'delivered', 'cancelled'])->default('received');
            $table->enum('priority', ['low', 'medium', 'high', 'urgent'])->default('medium');
            $table->text('notes')->nullable();
            $table->text('customer_reported_issues')->nullable();
            $table->text('vehicle_condition_notes')->nullable();
            $table->string('keys_location')->nullable();
            $table->boolean('personal_items_removed')->default(false);
            $table->boolean('photos_taken')->default(false);
            $table->timestamp('completion_datetime')->nullable();
            $table->enum('delivery_method', ['pickup', 'delivery', 'towing'])->nullable();
            $table->integer('customer_satisfaction_rating')->nullable();
            $table->timestamps();

            $table->index(['branch_code', 'status']);
            $table->index(['customer_id', 'vehicle_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('job_cards');
    }
};
