<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdateCarRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            // Branch
            'branch_id' => 'required|exists:branches,id',

            // Core Identity
            'brand_id' => 'required|exists:brands,id',
            'car_model_id' => 'required|exists:car_models,id',
            'trim_id' => 'required|exists:trims,id',
            'year' => 'required|integer|min:2000|max:'.(date('Y') + 1),
            'title' => 'required|string|max:255',
            'type' => 'required|in:sale,rent,both',
            'body_type' => 'required|in:sedan,suv,crossover,hatchback,coupe,pickup,van,convertible',

            // Technical
            'fuel_type' => 'required|in:petrol,diesel,electric,hybrid',
            'transmission' => 'required|in:automatic,manual',
            'drive_type' => 'required|in:FWD,RWD,AWD',
            'engine_size' => 'nullable|string',
            'horsepower' => 'nullable|integer',
            'seats' => 'required|integer|min:1',
            'doors' => 'required|integer|min:1',

            // Usage
            'mileage' => 'required|integer|min:0',
            'condition' => 'required|in:new,used',
            'color_ext' => 'nullable|string',
            'color_int' => 'nullable|string',
            'vin' => 'nullable|string',
            'plate_number' => 'nullable|string',

            // Commercial (Sale)
            'price' => 'nullable|numeric|min:0',
            'is_installment_available' => 'nullable|boolean',
            'first_payment' => 'nullable|numeric|min:0',
            'installment_months' => 'nullable|integer|min:1',

            // Commercial (Rent)
            'daily_price' => 'nullable|numeric|min:0',
            'weekly_price' => 'nullable|numeric|min:0',
            'monthly_price' => 'nullable|numeric|min:0',
            'insurance_deposit' => 'nullable|numeric|min:0',
            'max_mileage_daily' => 'nullable|integer|min:0',

            // Depreciation & Financial
            'purchase_price' => 'nullable|numeric|min:0',
            'purchase_date' => 'nullable|date',
            'salvage_value' => 'nullable|numeric|min:0',
            'useful_life_years' => 'nullable|integer|min:1',
            'depreciation_method' => 'nullable|in:straight_line,declining_balance,units_of_production',
            'depreciation_rate' => 'nullable|numeric|min:0|max:1',
            'total_expected_mileage' => 'nullable|integer|min:0',
            'is_leased' => 'nullable|boolean',

            // General
            'status' => 'required|in:available,reserved,rented,sold,maintenance',
            'description' => 'nullable|string',
            'features' => 'nullable|string',
            'delete_images' => 'nullable|array',
            'delete_images.*' => 'exists:car_images,id',
            'images.*' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048',

            // Add-ons
            'add_ons' => 'nullable|array',
            'add_ons.*' => 'exists:add_ons,id',
            'add_on_custom_prices' => 'nullable|array',
            'add_on_custom_prices.*' => 'nullable|numeric|min:0',
            'suggested_bundle_id' => 'nullable|exists:add_ons,id',
        ];
    }

    protected function prepareForValidation()
    {
        $this->merge([
            'is_installment_available' => $this->has('is_installment_available'),
            'is_leased' => $this->has('is_leased'),
        ]);
    }
}
