<?php

namespace App\Modules\HR\EmployeeSelfService\Requests;

use App\Support\NotificationCategory;
use Illuminate\Foundation\Http\FormRequest;

class MeNotificationIndexRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    protected function prepareForValidation(): void
    {
        $m = [];
        foreach (['type'] as $key) {
            if ($this->input($key) === '') {
                $m[$key] = null;
            }
        }
        if ($m !== []) {
            $this->merge($m);
        }
    }

    /**
     * @return array<string, array<int, string>>
     */
    public function rules(): array
    {
        return [
            'type' => ['nullable', 'string', 'in:all,'.implode(',', NotificationCategory::all())],
        ];
    }

    public function filterCategory(): ?string
    {
        $t = $this->input('type', 'all');

        return $t === 'all' ? null : (string) $t;
    }
}
