<?php

namespace App\Http\Requests\API;

use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
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 email
 * @property mixed photo
 * @property mixed country_id
 * @property mixed city_id
 */
class AddEscortRequest 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()
    {
        $phonePattern = '/^(05){1}([0-9]{8})$/';
        $rules=[
            'name'=> 'required|min:3|max:50',
            'email' => [
                'required',
                'email'
            ],
            'country_id' => 'required|exists:countries,id',
            'phone'=> [
                "required",
                "numeric",
                "regex:".$phonePattern,
            ],
            'birth_date' => 'required|date',
            'gender' => 'required|in:male,female',
            'city_id' => 'required|exists:cities,id',
            'photo' => 'nullable|image|mimes:jpg,jpeg,png',
        ];

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $field = metaFields('members');
        $local = getCurrentLocale();
        $attributes= [
            'name'=> $field[$local]['name'] ?? __('members.name'),
            'email'=> $field[$local]['email'] ?? __('members.email'),
            'country_id'=> $field[$local]['country_id'] ?? __('members.country_id'),
            'phone'=> $field[$local]['phone'] ?? __('members.phone'),
            'birth_date'=> $field[$local]['birth_date'] ?? __('members.birth_date'),
            'gender'=> $field[$local]['gender'] ?? __('members.gender'),
            'city_id'=> $field[$local]['city_id'] ?? __('members.city_id'),
            'photo'=> $field[$local]['photo'] ?? __('members.photo'),
            'password'=> $field[$local]['password'] ?? __('members.password'),
            'agreement'=> $field[$local]['agreement'] ?? __('members.agreement'),
            'password_confirmation'=> $field[$local]['password_confirmation'] ?? __('members.password_confirmation'),
        ];
        return $attributes;
    }

    public function messages()
    {
        return [
            'phone.regex' => __('members.phone_regex'),
        ];
    }

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