<?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 operation
 */
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()
    {
        $rules = [];
        $phonePattern = '/^(05){1}([0-9]{8})$/';
        $user = Auth::user();
        if (request('operation') == 'update_profile'){
            $rules=[
                'photo'=> [
                    'nullable',
                    'array',
//                    'image',
//                    'mimes:jpeg,png,jpg',
//                    'max:1000',
                ],
                'name'=> 'required|min:3|max:100',
                'email' => [
                    'required',
                    'email',
                    'min:3',
                    'max:50',
                    Rule::unique('users')->ignore($user->id, 'id')
                ],
                'city_id'=> 'required|exists:cities,id',
                'gender'=> 'required|in:male,female',
                'employer'=> 'nullable|min:3|max:100',

            ];
        }

        if (request('operation') == '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
     */
    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));
    }
}
