<?php

namespace App\Http\Requests\Api;

use App\Libs\Support\Sanitizer\Traits\SanitizesInput;
use Illuminate\Foundation\Http\FormRequest;

class UserFrontendWorkGroup extends FormRequest
{
    use SanitizesInput;

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(): array
    {
        $rules = [];

        $rules = array_merge($rules, [
            'wg'                    => 'array',
            'wg.*'                  => 'exists:work_groups,id',
            'wg_type'               => 'array',
            'wg_type.*'             => 'in:to,cc',
            'committees'            => 'array',
            'committees.*'          => 'exists:committees,id',
            'committees_type'       => 'array',
            'committees_type.*'     => 'in:to,cc',
            'addLegalOpinion'       => 'nullable|boolean'
        ]);

        return $rules;
    }

    /**
     * Configure the validator instance.
     *
     * @param \Illuminate\Contracts\Validation\Validator $validator
     */
    public function withValidator($validator)
    {
        $validator->after(function ($validator) {
            if ($this->has('wg')) {
                foreach ($this->wg as $index => $wg) {
                    if (!isset($this->wg_type[$index])) {
                        $validator->errors()->add("wg_type.$index", 'Missing type.');
                    }
                }
            }

            if ($this->has('wg_type')) {
                foreach ($this->wg_type as $index => $wg_type) {
                    if (!isset($this->wg[$index])) {
                        $validator->errors()->add("wg.$index", 'Missing Work Group.');
                    }
                }
            }

            if ($this->has('committees')) {
                foreach ($this->committees as $index => $committee) {
                    if (!isset($this->committees_type[$index])) {
                        $validator->errors()->add("committees_type.$index", 'Missing type.');
                    }
                }
            }

            if ($this->has('committees_type')) {
                foreach ($this->committees_type as $index => $committees_type) {
                    if (!isset($this->committees[$index])) {
                        $validator->errors()->add("committees.$index", 'Missing Work Group.');
                    }
                }
            }
        });
    }

    /**
     * Filters to be applied to the input.
     *
     * @return array
     */
    public function filters(): array
    {
        return [
            'wg'                    => "trim|strip_tags|xss_clean",
            'wg.*'                  => "trim|strip_tags|xss_clean",
            'wg_type'               => "trim|strip_tags|xss_clean",
            'wg_type.*'             => "trim|strip_tags|xss_clean",
            'committees'            => "trim|strip_tags|xss_clean",
            'committees.*'          => "trim|strip_tags|xss_clean",
            'committees_type'       => "trim|strip_tags|xss_clean",
            'committees_type.*'     => "trim|strip_tags|xss_clean",
            'addLegalOpinion'       => "trim|strip_tags|xss_clean",
        ];
    }
}
