<?php

namespace App\Modules\HR\Organization\Requests;

use App\Modules\HR\Organization\Models\CostCenter;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreCostCenterRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('create', CostCenter::class) ?? 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;
    }
}
