<?php

namespace Modules\User\Requests\Api;



use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class EditUserRequest extends FormRequest
{

    public function authorize()
    {
        // Check if the user is logged in and has the role equal to 4 or 6
        return auth()->check() && in_array(auth()->user()->role,[4,6]);
    }

    public function rules()
    {

        $userId = auth()->id();

        return [
            'name' => ['sometimes', 'required', 'string', 'min:3', 'max:15'],
            'last_name' => ['sometimes', 'required', 'string', 'min:3', 'max:15'],
            'photo' => ['sometimes', 'nullable', 'image', 'mimes:jpeg,png,jpg,gif,svg'],
            'email' => [
                'sometimes',
                'required',
                'email',
                'max:250',
                Rule::unique('users')->ignore($userId),
            ],
            'phone' => [
                'sometimes',
                'required',
                'regex:/^9665\d{8}$/',
                Rule::unique('users')->ignore($userId),
            ],
            'id_number' => [
                'sometimes',
                'required',
                'digits:10',
                Rule::unique('users')->ignore($userId),
            ],
            'current_password' => ['sometimes', 'string', 'current_password'],
            'password' => [
                'sometimes',
                'required',
                'string',
                'min:6',
                'regex:/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&#]).+$/',
                'confirmed',
            ],
        ];
    }


    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes(): array
    {
        return [
            'photo' => __('users.photo'),
            'name' => __('users.name'),
            'last_name' => __('users.last_name'),
            'email' => __('users.email'),
            'id_number' => __('users.id_number'),
            'phone' => __('users.phone'),
            'password' => __('users.password'),
            'confirmed' => __('users.password_confirmation'),
            'password_confirmation' => __('users.password_confirmation'),
            'current_password' => __('users.current_password'),
        ];
    }

    /**
     * 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)
    {
        $errors = (new ValidationException($validator))->errors();
        $message = $validator->errors()->first();
        throw new HttpResponseException(response()->json(['message' => $message, 'errors' => $errors], 400));
    }

}
