<?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 MeetingIndex 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 [
            'page'          => $this->input('page'),
            'perPage'       => $this->input('perPage'),
            'orderBy'       => $this->input('orderBy'),
            'orderDir'      => $this->input('orderDir'),
            'committeeId'   => $this->input('committeeId'),
            'workGroupId'   => $this->input('workGroupId'),
            'userFlag'      => $this->input('userFlag'),
            'nextMeetings'  => $this->input('nextMeetings'),
            'pastMeetings'  => $this->input('pastMeetings'),
        ];
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules():array
    {
        return [
            'page'          => "nullable|integer|min:1",
            'perPage'       => "nullable|integer|min:1|max:500",
            'orderBy'       => "nullable|in:title,date",
            'orderDir'      => "nullable|in:asc,desc",
            'committeeId'   => "nullable|numeric",
            'workGroupId'   => "nullable|numeric",
            'userFlag'      => "nullable|boolean",
            'nextMeetings'  => "nullable|boolean",
            'pastMeetings'  => "nullable|boolean",
        ];
    }

    public function filters() {
        return [
            'orderBy'      => "trim|strip_tags|xss_clean",
            'orderDir'     => "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);
    }
}
