<?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('bookings', function (Blueprint $table) {
            $table->id();
            $table->string('booking_ref')->unique();
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('openid')->nullable()->index();
            $table->foreignId('ev_route_id')->constrained('ev_routes')->cascadeOnDelete();
            $table->foreignId('departure_time_slot_id')->constrained('departure_time_slots')->cascadeOnDelete();
            $table->date('travel_date');
            $table->string('passenger_name');
            $table->string('passenger_phone');
            $table->string('pickup_address');
            $table->decimal('pickup_lat', 10, 7)->nullable();
            $table->decimal('pickup_lng', 10, 7)->nullable();
            $table->string('dropoff_address');
            $table->decimal('dropoff_lat', 10, 7)->nullable();
            $table->decimal('dropoff_lng', 10, 7)->nullable();
            // Total across all booking_vehicle_options lines — see that table for the
            // per-vehicle-option breakdown (a booking can mix e.g. front_seat + back_seat).
            $table->decimal('price', 10, 2);
            $table->string('status')->default('pending_payment');
            $table->boolean('is_round_trip')->default(false);
            $table->date('return_travel_date')->nullable();
            $table->string('created_by_channel');
            $table->timestamps();

            $table->index(['ev_route_id', 'travel_date', 'departure_time_slot_id']);
        });
    }

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