<?php

namespace App\Http\Requests\Web;

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

/**
 * @property mixed phone
 */
class UpdateProfileRequest extends FormRequest
{

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        if (! Auth::check()) {
            return false;
        }
        if(in_array(Auth::user()->role, ['user']))
        {
            return true;
        }
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [];
        $phonePattern = '/^(05){1}([0-9]{8})$/';
        if (request('update_profile')){
            $rules=[
                'photo'=> 'nullable|mimes:png,jpg,jpeg|max:1000',
                'name'=> 'required|min:3|max:100',
                'email' => [
                    'required',
                    'email',
                    'min:3|max:50',
                    Rule::unique('users')->ignore($this->user->id, 'id')
                ],
                'city_id'=> 'required|exists:cities,id',
                'gender'=> 'required|in:male,female',
                'employer'=> 'nullable|min:3|max:100',

            ];
        }
        if (request('update_phone')){
            $rules=[
                'phone'=> [
                    "required",
                    "numeric",
                    "regex:".$phonePattern,
                    Rule::unique('users'),
//            ->ignore($this->user->id, 'id')
                ],
            ];
        }
        return  $rules;
    }

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

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

    /**
     * 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 {
            parent::failedValidation($validator);
        }
    }
}
