<?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 FormEventRegistrationRequest extends FormRequest
{
    use SanitizesInput;

    public function authorize(): bool
    {
        return true;
    }

    public function validationData(): array
    {
        return [
            'eventRegistration'             => $this->input('eventRegistration'),
            'fullName'                      => $this->input('fullName'),
            'jobTitle'                      => $this->input('jobTitle'),
            'department'                    => $this->input('department'),
            'email'                         => $this->input('email'),
            'companyName'                   => $this->input('companyName'),
            'companyType'                   => $this->input('companyType'),
            'specialRequest'                => $this->input('specialRequest'),
            'specialRequestComment'         => $this->input('specialRequestComment'),
            'dinnerJoin'                    => $this->input('dinnerJoin'),
            'companyFullName'               => $this->input('companyFullName'),
            'companyAddress'                => $this->input('companyAddress'),
            'companyCountry'                => $this->input('companyCountry'),
            'companyVat'                    => $this->input('companyVat'),
            'invoiceEmail'                  => $this->input('invoiceEmail'),
            'orderNumber'                   => $this->input('orderNumber'),
            'additionalInformation'         => $this->input('additionalInformation'),
            'additionalInformationComment'  => $this->input('additionalInformationComment'),
            'agreePhotos'                   => $this->input('agreePhotos'),
            'agreeCheck'                    => $this->input('agreeCheck'),
            'tokenRecaptcha'                => $this->input('tokenRecaptcha'),
        ];
    }

    public function rules(): array
    {
        return [
            'eventRegistration'             => 'required|boolean',
            'fullName'                      => 'required|string|max:250',
            'jobTitle'                      => 'required|string|max:150',
            'department'                    => 'required|string|max:150',
            'email'                         => 'required|email|max:150',
            'companyName'                   => 'required|string|max:150',
            'companyType'                   => 'required|string|max:250',
            'specialRequest'                => 'required|boolean',
            'specialRequestComment'         => 'nullable|string',
            'dinnerJoin'                    => 'required|string|max:100',
            'companyFullName'               => 'required|string|max:255',
            'companyAddress'                => 'required|string|max:255',
            'companyCountry'                => 'required|string|max:50',
            'companyVat'                    => 'required|string|max:50',
            'invoiceEmail'                  => 'required|email|max:150',
            'orderNumber'                   => 'nullable|string|max:100',
            'additionalInformation'         => 'required|boolean',
            'additionalInformationComment'  => 'nullable|string',
            'agreePhotos'                   => 'required|boolean',
            'agreeCheck'                    => 'required|boolean',
            'tokenRecaptcha'                => 'string',
        ];
    }

    public function filters()
    {
        return [
            'eventRegistration'            => "trim|strip_tags|xss_clean",
            'fullName'                     => "trim|strip_tags|xss_clean",
            'jobTitle'                     => "trim|strip_tags|xss_clean",
            'department'                   => "trim|strip_tags|xss_clean",
            'email'                        => "trim|strip_tags|xss_clean",
            'companyName'                  => "trim|strip_tags|xss_clean",
            'companyType'                  => "trim|strip_tags|xss_clean",
            'specialRequest'               => "trim|strip_tags|xss_clean",
            'specialRequestComment'        => "trim|strip_tags|xss_clean",
            'dinnerJoin'                   => "trim|strip_tags|xss_clean",
            'companyFullName'              => "trim|strip_tags|xss_clean",
            'companyAddress'               => "trim|strip_tags|xss_clean",
            'companyCountry'               => "trim|strip_tags|xss_clean",
            'companyVat'                   => "trim|strip_tags|xss_clean",
            'invoiceEmail'                 => "trim|strip_tags|xss_clean",
            'orderNumber'                  => "trim|strip_tags|xss_clean",
            'additionalInformation'        => "trim|strip_tags|xss_clean",
            'additionalInformationComment' => "trim|strip_tags|xss_clean",
            'agreePhotos'                  => "trim|strip_tags|xss_clean",
            'agreeCheck'                   => "trim|strip_tags|xss_clean",
            'tokenRecaptcha'               => "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);
    }
}
