<?php

namespace App\Modules\HR\Organization\Requests;

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

class StoreTeamRequest extends FormRequest
{
    use PreparesOrganizationFormData;

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

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

        return $rules;
    }
}
