<?php

namespace App\Modules\HR\Organization\Requests;

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

class UpdateCostCenterRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('update', $this->route('cost_center')) ?? false;
    }

    public function rules(): array
    {
        $rules = OrganizationRequestRules::for('CostCenter');
        $rules['code'][] = Rule::unique('hr_cost_centers', 'code')->where(fn ($q) => $q->where('tenant_id', $this->user()->tenant_id))
            ->ignore($this->route('cost_center')?->id);

        return $rules;
    }
}
