<?php

namespace Modules\Routing\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

/**
 * Shape validation only for the routes search endpoint. round_trip=true
 * requires both from and to — "return" only means something for a specific
 * origin/destination pair, not an unfiltered route list.
 */
class SearchRoutesRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    /**
     * @return array<string, array<int, mixed>>
     */
    public function rules(): array
    {
        return [
            'company' => ['nullable', 'integer', 'exists:ev_companies,id'],
            'from' => ['nullable', 'integer', 'exists:destinations,id', 'required_if:round_trip,true'],
            'to' => ['nullable', 'integer', 'exists:destinations,id', 'different:from', 'required_if:round_trip,true'],
            'date' => ['nullable', 'date'],
            // The catalog's shared time value (e.g. "06:00"), not a
            // DepartureTimeSlot id — matches how customers think about
            // departure times (domain.md §1).
            'time_slot' => ['nullable', 'date_format:H:i'],
            'round_trip' => ['sometimes', 'boolean'],
            // Independent page cursors — routes and return_routes almost
            // always have different totals, so they can't share one `page`
            // without one side silently paginating the other's offset.
            'page' => ['nullable', 'integer', 'min:1'],
            'return_page' => ['nullable', 'integer', 'min:1'],
        ];
    }
}
