<?php

namespace App\Http\Requests;

use App\Rules\HasNoDuplicateEquipment;
use App\Rules\HasNoDuplicateFeatures;
use App\Rules\HasNoDuplicateSpecs;
use App\Rules\UniqueVehicleRegistrationNumber;
use App\Rules\ValidVehicleRegistrationNumber;
use Mtc\MercuryDataModels\VehicleType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use Mtc\MercuryDataModels\SeoData;
use Mtc\MercuryDataModels\VehicleTypeProfile;

class UpdateVehicleRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(): array
    {
        // Types that are not road-registered (e.g. machinery) don't require a
        // registration number. All current types expect registration, so this
        // is byte-identical today and pre-wires non-registering types.
        $type = $this->input('type', $this->vehicle?->type);
        $registrationRule = VehicleTypeProfile::for($type)->expectsRegistration()
            ? 'required_unless:is_new,true'
            : 'nullable';

        return [
            'type' => ['sometimes', Rule::enum(VehicleType::class)],
            'vin' => 'sometimes',
            'title' => 'required',
            'derivative' => 'required',
            'registration_number' => [
                $registrationRule,
                new UniqueVehicleRegistrationNumber($this->vehicle?->id),
                new ValidVehicleRegistrationNumber(
                    $this->input('personalized_number_plate', false),
                    $this->input('is_new', false)
                )
            ],
            'is_new' => 'sometimes',
            'is_demo' => 'sometimes',
            'is_sold' => 'sometimes',
            'srp_featured' => 'sometimes|boolean',
            'hide_finance_calculator' => 'sometimes|boolean',
            'is_published' => [
                'present',
                'boolean',
            ],
            'make_id' => [
                'required',
                'numeric'
            ],
            'model_id' => [
                'required',
                'numeric'
            ],
            'dealership_id' => [
                'nullable',
                'numeric'
            ],
            'transmission_id' => [
                'nullable',
                'numeric'
            ],
            'fuel_type_id' => [
                'nullable',
                'numeric'
            ],
            'drivetrain_id' => [
                'nullable',
                'numeric'
            ],
            'colour' => [
                'nullable',
            ],
            'price' => [
                'nullable',
                'numeric'
            ],
            'original_price' => [
                'nullable',
                'numeric'
            ],
            'monthly_price' => [
                'nullable',
                'numeric'
            ],
            'rrp_price' => [
                'nullable',
                'numeric'
            ],
            'admin_fee' => [
                'nullable',
                'numeric'
            ],
            'previous_owner_count' => [
                'nullable',
                'numeric',
            ],
            'deposit' => [
                'nullable',
                'numeric'
            ],
            'alternate_finance_key' => [
                'nullable',
                'string',
                'max:500',
            ],
            'door_count' => [
                'nullable',
                'numeric'
            ],
            'seats' => [
                'nullable',
                'numeric'
            ],
            'manufacture_year' => [
                'nullable',
                'numeric'
            ],
            'odometer_km' => [
                'nullable',
                'numeric'
            ],
            'odometer_mi' => [
                'nullable',
                'numeric'
            ],
            'engine_size_cc' => [
                'nullable',
                'numeric'
            ],
            'co2' => [
                'nullable',
                'numeric'
            ],
            'mpg' => [
                'nullable',
                'numeric'
            ],
            'first_registration_date' => [
                'nullable',
                'date'
            ],
            'description' => [
                'sometimes',
            ],
            'description_for_feeds' => [
                'sometimes',
                'nullable',
                'string',
            ],
            'media' => [
                'array'
            ],
            'media.*' => [
                'numeric',
                'exists:media,id'
            ],
            'primary_media' => [
                'present'
            ],
            'features' => [
                new HasNoDuplicateFeatures()
            ],
            'equipment' => [
                'array',
                new HasNoDuplicateEquipment()
            ],
            'equipment.*.code' => 'sometimes',
            'equipment.*.description' => [
                'sometimes',
                'required'
            ],
            'standard_equipment.*.type' => [
                'required',
            ],
            'standard_equipment.*.price' => [
                'nullable',
                'numeric',
            ],
            'specs' => [
                'array',
                new HasNoDuplicateSpecs()
            ],
            'specs.*.code' => 'sometimes',
            'specs.*.category' => 'sometimes',
            'specs.*.description' => [
                'required'
            ],
            'specs.*.value' => [
                'required'
            ],
            'seo.jsonSchema' => 'json',
            'seo.title' => [
                'nullable',
                'min:' . SeoData::TITLE_MIN_LENGTH,
                'max:' . SeoData::TITLE_MAX_LENGTH,
            ],
            'seo.description' => [
                'nullable',
                'min:' . SeoData::DESCRIPTION_MIN_LENGTH,
                'max:' . SeoData::DESCRIPTION_MAX_LENGTH,
            ],
        ];
    }

    #[\Override]
    public function attributes()
    {
        return [
            'seo.jsonSchema' => 'JSON schema',
            'is_new' => 'new vehicle',
            'make_id' => 'vehicle make',
            'model_id' => 'vehicle model',
            'dealership_id' => 'dealership',
            'transmission_id' => 'transmission',
            'fuel_type_id' => 'fuel type',
            'drivetrain_id' => 'drivetrain',
            'seo.title' => 'SEO page title',
            'seo.description' => 'SEO page description',
        ];
    }

    public function primaryMedia()
    {
        if (!empty($this->input('primary_media'))) {
            return $this->input('primary_media');
        }

        return collect($this->input('media', []))
            ->flip()
            ->first();
    }
}
