<?php

namespace App\Http\Requests\Admin;

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

class MenuUpdateRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user('admin')?->can('manage menus') ?? false;
    }

    public function rules(): array
    {
        $menuId = $this->route('menu')?->id;
        return [
            'slug' => [
                'required',
                'regex:/^[a-z0-9_-]{1,64}$/',
                Rule::unique('menus')->where('brand_id', $this->input('brand_id'))->ignore($menuId),
            ],
            'name'      => ['required', 'string', 'max:120'],
            'brand_id'  => ['nullable', 'integer', 'exists:brands,id'],
            'is_active' => ['boolean'],
        ];
    }
}
