<?php

namespace App\Http\Requests\Badges;

use Illuminate\Foundation\Http\FormRequest;

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

    protected function prepareForValidation(): void
    {
        $userIds = $this->input('user_ids', []);

        if (is_string($userIds)) {
            $userIds = array_filter(array_map('trim', explode(',', $userIds)), 'strlen');
        } elseif (!is_array($userIds)) {
            $userIds = $userIds ? [$userIds] : [];
        }

        $this->merge([
            'user_ids' => array_values(array_unique(array_map(static fn ($v) => (int) $v, $userIds))),
        ]);
    }

    public function rules(): array
    {
        return [
            'user_ids' => ['nullable', 'array'],
            'user_ids.*' => ['integer', 'min:1'],
            'school_year' => ['nullable', 'string', 'max:50'],
            'active_role' => ['nullable', 'string', 'in:teacher,ta,admin,staff'],
        ];
    }

    public function normalizedUserIds(): array
    {
        return $this->input('user_ids', []);
    }
}