<?php

namespace App\Http\Requests\Api;

use App\Libs\Security\XSSCleanFilter;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use App\Libs\Support\Sanitizer\Traits\SanitizesInput;
use Illuminate\Validation\ValidationException;

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

    public function validationData(): array
    {
        return [
            'name'          => $this->input('name'),
            'lastName'      => $this->input('lastName'),
            'department'    => $this->input('department'),
            'function'      => $this->input('function'),
            'phoneNumber'   => $this->input('phoneNumber'),
        ];
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules():array
    {
        return [
            'name'          => 'required|string|max:50',
            'lastName'      => 'required|string|max:50',
            'department'    => 'nullable|string|max:250',
            'function'      => 'nullable|string|max:250',
            'phoneNumber'   => 'nullable|string|max:20',
        ];
    }

    public function filters() {
        return [
            'name'          => "trim|strip_tags|xss_clean",
            'lastName'      => "trim|strip_tags|xss_clean",
            'department'    => "trim|strip_tags|xss_clean",
            'function'      => "trim|strip_tags|xss_clean",
            'phoneNumber'   => "trim|strip_tags|xss_clean",
        ];
    }

    public function customFilters()
    {
        return ['xss_clean' => XSSCleanFilter::class];
    }

    protected function failedValidation(Validator $validator)
    {
        throw (new ValidationException($validator))
            ->errorBag($this->errorBag);
    }
}
