<?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::create('bookings', function (Blueprint $table) {
            $table->id();
            $table->string('booking_number')->unique();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->foreignId('customer_id')->constrained();
            $table->foreignId('driver_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('vehicle_type_id')->constrained();
            $table->foreignId('status_id')->constrained('booking_statuses');
            $table->foreignId('payment_method_id')->nullable()->constrained();
            $table->foreignId('currency_id')->constrained();

            $table->string('pickup_address', 800);
            $table->decimal('pickup_lat', 10, 7)->nullable();
            $table->decimal('pickup_lng', 10, 7)->nullable();
            $table->string('pickup_note')->nullable();
            $table->string('dropoff_address', 800);
            $table->decimal('dropoff_lat', 10, 7)->nullable();
            $table->decimal('dropoff_lng', 10, 7)->nullable();
            $table->string('dropoff_note')->nullable();

            $table->dateTime('pickup_datetime');
            $table->unsignedTinyInteger('passengers')->default(1);
            $table->unsignedTinyInteger('luggage')->default(0);
            $table->string('flight_number')->nullable();
            $table->string('customer_reference')->nullable();
            $table->text('notes')->nullable();

            $table->decimal('distance', 8, 2)->nullable();
            $table->enum('distance_unit', ['km', 'mi'])->default('mi');
            $table->unsignedInteger('duration_minutes')->nullable();
            $table->decimal('fare_amount', 10, 2)->nullable();

            $table->enum('job_type', ['controller', 'passenger', 'driver'])->default('controller');
            $table->foreignId('assigned_by')->nullable()->constrained('users')->nullOnDelete();
            $table->dateTime('assigned_at')->nullable();

            $table->timestamps();
            $table->softDeletes();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('bookings');
    }
};
