<?php

namespace App\Modules\HR\Organization\Requests;

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

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

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

        return $rules;
    }
}
