<?php

namespace App\Http\Requests\User;

use App\Enums\GenderType;
use App\Enums\RolSpatie;
use App\Http\Messages\ErrorMessages;
use App\Http\Responses\ApiResponse;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Validation\Rules\Enum;

class PatchUserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'name' => 'sometimes|string|max:255',
            'last_name' => 'sometimes|string|max:255',
            'email' => 'required|email|unique:users,email,' . $this->route('id'),
            'cod_phone' => 'required|string',
            'phone' => 'required|string|unique:users,phone,'. $this->route('id'),
            'position' => 'sometimes|string',
            'branch_id' => 'sometimes',
            'gender' => ['sometimes', new Enum(GenderType::class)],
            'rol'=>['sometimes', new Enum(RolSpatie::class)],
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        throw new HttpResponseException(
            ApiResponse::error(
                ErrorMessages::UNPROCESSABLE_ENTITY,
                $validator->errors(),
                [],
                422
            )
        );
    }
}
