<?php

namespace App\Http\Requests\Web;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

/**
 * @property mixed password
 * @property mixed active_way
 * @property mixed documentation_id
 * @property mixed coupon_code
 * @property mixed type
 * @property mixed city_id
 */
class DocumentationBookingRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::check();
//        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules=[
            'type' => 'required|in:inside_office,outside_office',
            'city_id' => 'required_if:type,outside_office|nullable|exists:cities,id',
            'documentation_type_id' => 'required|exists:documentation_types,id',
            'notes' => 'required|min:3|max:400',
            'lat' => 'required_if:type,outside_office|nullable|numeric',
            'lng' => 'required_if:type,outside_office|nullable|numeric',
            'address' => 'required_if:type,outside_office|nullable|min:3|max:200',
            'documentation_date' => 'required|exists:documentations,documentation_date|date|after_or_equal:'.date('Y-m-d'),
            'documentation_time' => 'required|regex:/[0-9]{1,2}:[0-9]{1,2}/',
            'pay_way' => 'required|in:14,11,2,6',
            'coupon_code' => 'nullable|exists:coupons,code',
            'documentation_id' => 'required|exists:documentations,id',
        ];

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $field = metaFields('documentationRequests');
        $local = getCurrentLocale();
        $attributes= [
            'type'=> $field[$local]['type'] ?? __('documentationRequests.type'),
            'city_id'=> $field[$local]['city_id'] ?? __('documentationRequests.city_id'),
            'documentation_type_id'=> $field[$local]['documentation_type_id'] ?? __('documentationRequests.documentation_type_id'),
            'notes'=> $field[$local]['notes'] ?? __('documentationRequests.notes'),
            'lat'=> $field[$local]['lat'] ?? __('documentationRequests.lat'),
            'lng'=> $field[$local]['lng'] ?? __('documentationRequests.lng'),
            'address'=> $field[$local]['address'] ?? __('documentationRequests.address'),
            'documentation_date'=> $field[$local]['documentation_date'] ?? __('documentationRequests.documentation_date'),
            'documentation_time'=> $field[$local]['documentation_time'] ?? __('documentationRequests.documentation_time'),
            'pay_way'=> $field[$local]['pay_way'] ?? __('documentationRequests.pay_way'),
            'coupon_code'=>  __('documentationRequests.coupon_code'),
            'documentation_id'=> $field[$local]['documentation_id'] ?? __('documentationRequests.documentation_id'),
        ];
        return $attributes;
    }

    public function messages()
    {
        return [
            'phone.digits' => __('documentationRequests.phone_digits'),
            'phone.regex' => __('documentationRequests.phone_regex'),
            'city_id.required_if' => __('documentationRequests.city_id_required'),
            'address.required_if' => __('documentationRequests.address_required_if'),
            'lat.required_if' => __('documentationRequests.lat_lng_required_if'),
            'lat.numeric' => __('documentationRequests.lat_lng_required_if'),
            'lng.required_if' => __('documentationRequests.lat_lng_required_if'),
            'lng.numeric' => __('documentationRequests.lat_lng_required_if'),
        ];
    }

    /**
     * Handle a failed validation attempt.
     *
     * Override the parent method action if the request is for AJAX
     *
     * @param Validator $validator
     * @return void
     *
     * @throws ValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        // Change response attitude if the request done via Ajax requests like API requests
        if(Request::ajax()){
            if ($validator->fails()){
                $errors = $validator->errors()->first();
                throw new HttpResponseException(response()->json(['errors' => $errors]));
            }

        } else {
            // Do the original action of the Form request class
            parent::failedValidation($validator);
        }
    }
}
