<?php

namespace App\Modules\HR\Recruitment\Services;

use App\Models\User;
use App\Modules\HR\Departments\Repositories\DepartmentRepository;
use App\Modules\HR\Recruitment\DTOs\RecruitmentReportFilters;
use App\Modules\HR\Recruitment\Repositories\RecruitmentReportSavedFilterRepository;
use App\Modules\HR\Recruitment\Repositories\RecruitmentVacancyRepository;
use App\Modules\HR\Recruitment\Support\RecruitmentReportType;

class RecruitmentReportsPageService
{
    public function __construct(
        private readonly RecruitmentReportRegistry $registry,
        private readonly RecruitmentReportDashboardService $dashboard,
        private readonly RecruitmentReportSavedFilterRepository $savedFilters,
        private readonly DepartmentRepository $departments,
        private readonly RecruitmentVacancyRepository $vacancies,
    ) {}

    /** @return array<string, mixed> */
    public function hub(int $tenantId, int $userId, RecruitmentReportFilters $filters): array
    {
        return [
            'kpis' => $this->dashboard->cards($tenantId, $filters),
            'reports' => $this->catalog(),
            'filterOptions' => $this->filterOptions($tenantId),
            'filters' => $filters,
        ];
    }

    /** @return array<string, mixed> */
    public function show(string $type, int $tenantId, int $userId, RecruitmentReportFilters $filters): array
    {
        abort_unless(RecruitmentReportType::isValid($type), 404);
        $report = $this->registry->build($type, $tenantId, $filters);

        return [
            'type' => $type,
            'report' => $report,
            'kpis' => $report['summary'],
            'filterOptions' => $this->filterOptions($tenantId),
            'filters' => $filters,
            'savedFilters' => $this->savedFilters->forUserReport($tenantId, $userId, $type),
            'reports' => $this->catalog(),
        ];
    }

    /** @return list<array{type: string, icon: string}> */
    public function catalog(): array
    {
        return [
            ['type' => RecruitmentReportType::FUNNEL, 'icon' => 'fe fe-filter'],
            ['type' => RecruitmentReportType::OPEN_VACANCIES, 'icon' => 'fe fe-briefcase'],
            ['type' => RecruitmentReportType::CANDIDATE_PIPELINE, 'icon' => 'fe fe-git-branch'],
            ['type' => RecruitmentReportType::APPLICATIONS_BY_STATUS, 'icon' => 'fe fe-file-text'],
            ['type' => RecruitmentReportType::INTERVIEW_RESULTS, 'icon' => 'fe fe-clipboard'],
            ['type' => RecruitmentReportType::OFFER_ACCEPTANCE, 'icon' => 'fe fe-mail'],
            ['type' => RecruitmentReportType::HIRING, 'icon' => 'fe fe-user-plus'],
            ['type' => RecruitmentReportType::TIME_TO_HIRE, 'icon' => 'fe fe-clock'],
            ['type' => RecruitmentReportType::SOURCE_ANALYSIS, 'icon' => 'fe fe-pie-chart'],
        ];
    }

    /** @return array<string, mixed> */
    private function filterOptions(int $tenantId): array
    {
        $recruiters = User::query()
            ->where('tenant_id', $tenantId)
            ->orderBy('name')
            ->pluck('name', 'id')
            ->all();

        return [
            'departments' => $this->departments->listForSelect(),
            'vacancies' => $this->vacancies->listForSelect($tenantId),
            'recruiters' => $recruiters,
        ];
    }
}
