<?php

namespace Modules\Booking\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
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'],
            'notes' => ['nullable', 'string', 'max:1000'],
            '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'],
            // Round trip = a second, independently-priced leg on its own
            // route/time-slot/date — the return route must already exist as
            // a catalog EvRoute and is validated server-side as the true
            // reverse of ev_route_id (EvRoute::isReverseOf, domain.md §2b).
            'is_round_trip' => ['sometimes', 'boolean'],
            'return_ev_route_id' => ['required_if:is_round_trip,true', 'integer', 'exists:ev_routes,id'],
            'return_departure_time_slot_id' => ['required_if:is_round_trip,true', 'integer', 'exists:departure_time_slots,id'],
            'return_travel_date' => ['required_if:is_round_trip,true', 'date', 'after_or_equal:travel_date'],
            'return_selections' => ['required_if:is_round_trip,true', 'array', 'min:1'],
            'return_selections.*.vehicle_option' => ['required_if:is_round_trip,true', Rule::enum(VehicleOption::class)],
            'return_selections.*.passenger_count' => ['required_if:is_round_trip,true', 'integer', 'min:1'],
        ];
    }
}
