<?php

namespace App\Http\Requests\Menu;

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

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

    /**
     * @return array<string, ValidationRule|array<int, mixed>|string>
     */
    public function rules(): array
    {
        $menuId = $this->route('menu')?->id;

        return [
            'label' => ['required', 'string', 'max:100'],
            'route' => ['nullable', 'string', 'max:120'],
            'link' => ['nullable', 'string', 'max:200'],
            'icon' => ['nullable', 'string', 'max:60'],
            'section' => ['nullable', 'string', 'max:60'],
            'menu_type_id' => ['required', Rule::exists('menu_types', 'id')],
            'parent_id' => ['nullable', Rule::exists('menus', 'id')->where(fn ($q) => $q->where('id', '!=', $menuId))],
            'sort_order' => ['nullable', 'integer', 'min:0'],
            'status' => ['nullable', 'boolean'],
            'groups' => ['nullable', 'array'],
            'groups.*' => ['integer', Rule::exists('groups', 'id')],
        ];
    }
}
