<?php

namespace App\Http\Requests\Rental;

use Illuminate\Foundation\Http\FormRequest;

class CloseRentalRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true; // Controlled by Policy
    }

    public function rules(): array
    {
        return [
            'end_odometer' => 'required|integer|gt:start_odometer_hidden',
            'fuel_level_in' => 'required|integer|min:0|max:100',
            'violations' => 'nullable|array',
            'violations.*.description' => 'required|string',
            'violations.*.amount' => 'required|numeric|min:0',
            'violations.*.status' => 'required|in:pending,paid,deducted',
        ];
    }

    protected function prepareForValidation()
    {
        $rental = $this->route('rental');
        if ($rental) {
            $this->merge([
                'start_odometer_hidden' => $rental->start_odometer,
            ]);
        }
    }
}
