<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * One row per Vehicle Option selected on a booking — a booking can mix
     * e.g. front_seat + back_seat in one go (domain.md §2). The unique
     * constraint keeps each option to a single line per booking (no
     * duplicate front_seat rows); BookingService still enforces the
     * business rules (front-seat max, disabled options, whole-vehicle
     * exclusivity) on top of this shape.
     *
     * Also the natural source table for the deferred real-capacity-check
     * phase (domain.md §7): summing passenger_count per vehicle_option for
     * a route/date/time-slot is exactly what that future check needs.
     */
    public function up(): void
    {
        Schema::create('booking_vehicle_options', function (Blueprint $table) {
            $table->id();
            $table->foreignId('booking_id')->constrained('bookings')->cascadeOnDelete();
            $table->string('vehicle_option');
            $table->unsignedInteger('passenger_count')->default(1);
            // Snapshotted at booking time, same as bookings.price — never re-read
            // from route_pricing later (domain.md §3).
            $table->decimal('unit_price', 10, 2);
            $table->decimal('line_total', 10, 2);
            $table->timestamps();

            $table->unique(['booking_id', 'vehicle_option']);
            $table->index('vehicle_option');
        });
    }

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