<?php

namespace App\Http\Requests\Rental;

use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        return [
            'branch_id' => 'nullable|exists:branches,id',
            'car_id' => 'required|exists:cars,id',
            'customer_id' => 'required|exists:customers,id',
            'start_date' => 'required|date|after_or_equal:today',
            'end_date' => 'required|date|after:start_date',
            'start_odometer' => 'required|integer|min:0',
            'fuel_level_out' => 'required|integer|min:0|max:100',
            'daily_price' => 'required|numeric|min:0',
            'discount' => 'nullable|numeric|min:0',
            'vat_percentage' => 'nullable|numeric|min:0',
            'tax' => 'nullable|numeric|min:0',
            'insurance_amount' => 'nullable|numeric|min:0',
            'insurance_notes' => 'nullable|string',
            'deposit_type' => 'nullable|in:cash,transfer,check',
            'late_fee_per_day' => 'nullable|numeric|min:0',
            'contract_source' => 'nullable|in:direct,booking,repeat_customer',
            'allowed_km_per_day' => 'required|integer|min:0',
            'extra_km_price' => 'required|numeric|min:0',
            'addons' => 'nullable|array',
            'addons.*.name' => 'required|string',
            'addons.*.price' => 'required|numeric|min:0',
            'selected_addons' => 'nullable|array',
            'selected_addons.*' => 'exists:add_ons,id',
            'payment' => 'required|array',
            'payment.amount' => 'required|numeric|min:0',
            'payment.method' => 'required|in:cash,card,transfer,mada,visa,stcpay',
            'payment.type' => 'required|in:deposit,partial,full,refund',
            'payment.notes' => 'nullable|string',
            'inspection.odometer' => 'nullable|integer|min:0',
            'inspection.fuel_level' => 'nullable|in:quarter,half,three_quarters,full',
            'inspection.exterior_condition' => 'nullable|in:good,scratches,damages',
            'inspection.interior_condition' => 'nullable|in:good,damaged',
            'inspection.tires_condition' => 'nullable|in:good,worn,damaged',
            'inspection.spare_tire_available' => 'nullable|boolean',
            'inspection.tools_available' => 'nullable|boolean',
            'inspection.scratches_description' => 'nullable|string',
            'inspection.damages_description' => 'nullable|string',
            'inspection.inspection_notes' => 'nullable|string',
            'rental_terms_accepted' => 'required|accepted',
            'additional_terms' => 'nullable|array',
            'additional_terms.*' => 'nullable|string',
            'paper_contract' => 'nullable|file|mimes:pdf,jpg,jpeg,png|max:10240',
        ];
    }

    public function messages(): array
    {
        return [
            'rental_terms_accepted.required' => 'يجب قراءة وقبول شروط الاستئجار',
            'rental_terms_accepted.accepted' => 'يجب قراءة وقبول شروط الاستئجار',
        ];
    }
}
