<?php

namespace App\Http\Requests;

use App\Models\PurchaseUnit;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdatePurchaseUnitRequest extends FormRequest
{
    public function authorize(): bool
    {
        return (bool) $this->user()?->can('purchase-units.update');
    }

    /**
     * @return array<string, array<int, mixed>>
     */
    public function rules(): array
    {
        /** @var PurchaseUnit $unit */
        $unit = $this->route('purchaseUnit');

        return [
            'name' => [
                'required',
                'string',
                'max:255',
                Rule::unique(PurchaseUnit::class, 'name')->ignore($unit->id),
            ],
        ];
    }
}
