<?php

namespace App\Http\Requests;

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

class UserFrontendEdit extends FormRequest
{
    use SanitizesInput;

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

        $rules = array_merge($rules, [
            'name'              => 'required|max:50',
            'lastName'          => 'required|max:50',
            'department'        => 'nullable|max:250',
            'function'          => 'nullable|max:250',
            'phoneNumber'       => 'nullable|max:20',
            'password'          => 'nullable|'.config('auth.password_frontend_validation_rule').'|strong_password',
            'state'             => 'in:pendingApproval,block,active,inactive',
            'wg'                => 'array',
            'wg.*'              => 'exists:work_groups,id',
            'wg_type'           => 'array',
            'wg_type.*'         => 'in:to,cc',
            'legal_opinion_permission' => 'nullable',
            'finance_sub_committee_permission' => 'nullable',
        ]);

        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.');
                    }
                }
            }
        });
    }

    /**
     * Filters to be applied to the input.
     *
     * @return array
     */
    public function filters(): array
    {
        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",
            'state'             => "trim|strip_tags|xss_clean",
            '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",
            'phoneNumber'       => "trim|strip_tags|xss_clean",
            'password'          => "trim|strip_tags|xss_clean",
            'legal_opinion_permission' => "trim|strip_tags|xss_clean",
            'finance_sub_committee_permission' => "trim|strip_tags|xss_clean",
        ];
    }
}
