<?php

namespace App\Http\Requests\Web;

use Illuminate\Validation\Rule;
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 video_type
 * @property mixed video_url
 * @property mixed images
 */
class ProfileUserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $max_date = date('Y-m-d',strtotime('-18 years'));
        $rules =
            [
                'name'=>[
                    "required",
                    "max:20"
                ],
                'email'=>[
                    "required",
                    "email",
                    Rule::unique('users')->ignore(Auth::user()->id)
                ],
                // 'date_of_birth'=>'required|date|before_or_equal:'.$max_date,
                'gender'=>'required|in:male,female',
                'phone'=> [
                    "required",
                    "numeric",
                    "min_digits:9",
                    Rule::unique('users')->ignore(Auth::user()->id)
                ],
                'country_id'=>[
                    "required",
                    Rule::exists('countries', 'id')->whereNull('deleted_at')
                ],
                'city_id'=>[
                    "required",
                    Rule::exists('cities', 'id')->whereNull('deleted_at')
                ],
                'password'=>'sometimes|confirmed|nullable|regex:/^.*(?=.*[a-zA-Z]).*$/|min:6',
        ];

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $attributes=
            [
                'name' => __('web.name'),
                'email' => __('web.email'),
                'phone' => __('web.phone'),
                'date_of_birth' => __('web.date_of_birth'),
                'gender' => __('web.gender'),
                'country_id' => __('web.country_id'),
                'city_id' => __('web.city_id'),
                'password' => __('web.password'),
            ];

        foreach (languages() as $lang) {
            $lang_attributes=[
                'address_'.$lang->local=>  __('web.address') .' '.$lang->local,
            ];
            $attributes= array_merge($attributes, $lang_attributes);
        }
        return $attributes;
    }


    /**
     * 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::wantsJson()){
            $errors = (new ValidationException($validator))->errors();
            throw new HttpResponseException(response()->json(['errors' => $errors], 422));
        } else {
            parent::failedValidation($validator);
        }
    }
}
