<?php

namespace Modules\User\Requests\Employee;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Enums\UserTargetEnum;

class ProfileRequest 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 =
        [
            'name'=>
            [
                'required',
                'string'
            ],
            'email'=>
            [
                'required',
                'email',
                Rule::unique('users','email')->ignore(auth()->user()->id ?? null)
            ],
            'phone'=>
            [
                'nullable',
                'numeric',
                Rule::unique('users','phone')->ignore(auth()->user()->id ?? null)
            ],
            'phone_country_id'=>
            [
                'required',
                Rule::exists('countries','id')->whereNull('deleted_at')
            ],
            'password'=>
            [
                'sometimes',
                'confirmed'
            ],

        ];

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {

        $attributes= [
            'name'=>__('users.name'),
            'email'=>__('users.email'),
            'phone'=>__('users.phone'),
            'photo'=>__('users.photo'),
            'role'=>__('users.role'),
            'phone_country_id'=>__('users.phone_country_id'),
            'sector_id'=>__('users.sector_id'),
            'password'=>__('users.password'),
            'password_confirmation'=>__('users.password_confirmation'),
            'target_status'=>__('employees.target_status'),
            'target'=>__('employees.target'),
            'manager_id'=>__('employees.manager_id')
        ];

        return $attributes;
    }


    /**
     * Handle a failed validation attempt.
     *
     * Override the parent method action if the request is for AJAX
     *
     * @param \Illuminate\Contracts\Validation\Validator $validator
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        // 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));

    }
}
