<?php

namespace App\Http\Requests;

use App\Tier;
use App\TierHelper;
use Mtc\VehicleValuation\Facades\Valuation;

class ValuationRequest extends DealBuilderRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize(): bool
    {
        return parent::authorize()
            && TierHelper::isAllowed(tenant('tier'), Tier::PRO->value)
            && Valuation::hasEnabledDriver();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'registration_number' => [
                'required',
                'alpha_num',
            ],
            'mileage' => [
                'required',
                'numeric',
                'min:0',
            ],
            'email' => [
                'nullable',
                'email',
            ],
        ];
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'registration_number' => str_replace(' ', '', $this->input('registration_number'))
        ]);
    }
}
