<?php

declare(strict_types=1);

namespace App\Http\Requests\Academy;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpsertAcademyCategoryRequest extends FormRequest
{
    public function authorize(): bool
    {
        return (bool) $this->user()?->hasStaffAccess();
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'active' => $this->boolean('active', true),
            'order_num' => $this->filled('order_num') ? (int) $this->input('order_num') : 0,
        ]);
    }

    public function rules(): array
    {
        $categoryId = $this->route('academyCategory')?->id;

        return [
            'type' => ['required', 'string', Rule::in(['lesson', 'prompt', 'challenge', 'pack'])],
            'name' => ['required', 'string', 'max:120'],
            'slug' => ['required', 'string', 'max:140', Rule::unique('academy_categories', 'slug')->ignore($categoryId)],
            'description' => ['nullable', 'string'],
            'icon' => ['nullable', 'string', 'max:120'],
            'order_num' => ['required', 'integer', 'min:0', 'max:9999'],
            'active' => ['required', 'boolean'],
        ];
    }
}