<?php

namespace App\Http\Requests;

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

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

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

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