<?php

namespace App\Modules\Inventory\Requests;

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

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

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

        return [
            'name' => ['required', 'string', 'max:255'],
            'inventory_category_id' => [
                'nullable',
                'integer',
                Rule::exists('inventory_categories', 'id')->where(
                    static fn ($q) => $q->where('tenant_id', $tenantId),
                ),
            ],
            'barcode' => ['nullable', 'string', 'max:191'],
            'description' => ['nullable', 'string'],
        ];
    }
}
