<?php

namespace App\Modules\HR\Recruitment\Services;

use App\Modules\HR\Departments\Models\Department;
use App\Modules\HR\Recruitment\Repositories\RecruitmentJobRequestRepository;
use App\Modules\HR\Recruitment\Support\JobRequestStatus;
use App\Modules\HR\Recruitment\Support\VacancyStatus;
use Illuminate\Database\Eloquent\Collection;

class VacancyFormOptionsService
{
    public function __construct(
        private readonly RecruitmentJobRequestRepository $jobRequests,
    ) {}

    /** @return array<string, mixed> */
    public function forForm(int $tenantId): array
    {
        $approved = collect($this->jobRequests->searchForLookup(
            $tenantId,
            '',
            100,
            null,
            JobRequestStatus::APPROVED,
        ))->mapWithKeys(fn ($o) => [$o['value'] => $o['label']])->all();

        return [
            'departments' => $this->departments($tenantId),
            'jobRequestOptions' => $approved,
        ];
    }

    /** @return array<string, string> */
    public function statusOptions(): array
    {
        $opts = ['' => __('recruitment.filters.all')];
        foreach (VacancyStatus::all() as $s) {
            $opts[$s] = __('recruitment.vacancies.status.'.$s);
        }

        return $opts;
    }

    /** @return Collection<int, Department> */
    public function departments(int $tenantId): Collection
    {
        return Department::query()->where('tenant_id', $tenantId)->orderBy('name')->get();
    }
}
