<?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
 */
class UpdateProfileRequest 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()
    {
        $user = Auth::user();
        $phonePattern = '/^(05){1}([0-9]{8})$/';
        $rules=[
            'name'=> 'required|min:3',
            'email' => [
                'required',
                'email',
                Rule::unique('users')->ignore($user->id, 'id')
            ],
            'code' => 'required|exists:countries,code',
            'phone'=> [
                "required",
                "numeric",
                "regex:".$phonePattern,
                Rule::unique('users')->ignore($user->id, 'id')
            ],
            'birth_date' => 'required|date',
            'gender' => 'required|in:male,female',
            'city_id' => 'required|exists:cities,id',
            'photo' => 'nullable|image|mimes:jpg,jpeg,png',
            'password'=> 'required|confirmed',
//            'agreement'=> 'required|in:1',

        ];

        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'),
            'code'=> $field[$local]['code'] ?? __('members.code'),
            '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));
    }
}
