<?php

namespace Modules\Booking\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Modules\Booking\Enums\BookingChannel;
use Modules\Shared\Enums\VehicleOption;

/**
 * Shape validation only — business rules (front-seat limit, disabled vehicle
 * options, pricing) stay in BookingService/PricingService, not here.
 */
class StoreBookingRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    /**
     * @return array<string, array<int, mixed>>
     */
    public function rules(): array
    {
        return [
            'ev_route_id' => ['required', 'integer', 'exists:ev_routes,id'],
            'departure_time_slot_id' => ['required', 'integer', 'exists:departure_time_slots,id'],
            'travel_date' => ['required', 'date'],
            // One or more Vehicle Option lines — e.g. front_seat + back_seat together
            // (domain.md §2). Duplicate-option/whole-vehicle-exclusivity rules stay in
            // BookingService, not here.
            'selections' => ['required', 'array', 'min:1'],
            'selections.*.vehicle_option' => ['required', Rule::enum(VehicleOption::class)],
            'selections.*.passenger_count' => ['required', 'integer', 'min:1'],
            'passenger_name' => ['required', 'string', 'max:255'],
            'passenger_phone' => ['required', 'string', 'max:50'],
            'pickup_address' => ['required', 'string', 'max:500'],
            'pickup_lat' => ['nullable', 'numeric', 'between:-90,90'],
            'pickup_lng' => ['nullable', 'numeric', 'between:-180,180'],
            'dropoff_address' => ['required', 'string', 'max:500'],
            'dropoff_lat' => ['nullable', 'numeric', 'between:-90,90'],
            'dropoff_lng' => ['nullable', 'numeric', 'between:-180,180'],
            'openid' => ['nullable', 'string', 'max:255'],
            'is_round_trip' => ['sometimes', 'boolean'],
            'return_travel_date' => ['nullable', 'date', 'required_if:is_round_trip,true'],
            // Admin-created bookings go through the Filament resource (T4.7), not this API.
            'created_by_channel' => ['sometimes', Rule::enum(BookingChannel::class)->except(BookingChannel::Admin)],
        ];
    }
}
