<?php

namespace App\Modules\Inventory\Requests;

use App\Modules\Inventory\Models\ItemCategory;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreItemCategoryRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('create', ItemCategory::class) ?? false;
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        $tenantId = $this->user()?->tenant_id;

        return [
            'name' => ['required', 'string', 'max:255'],
            'parent_id' => [
                'nullable',
                'integer',
                Rule::exists('inventory_categories', 'id')->where(
                    static fn ($q) => $q->where('tenant_id', $tenantId),
                ),
            ],
            'sort_order' => ['nullable', 'integer', 'min:0'],
        ];
    }
}
