<?php

namespace App\Modules\HR\Recruitment\Services;

use App\Modules\HR\Recruitment\Repositories\RecruitmentDashboardRepository;
use App\Modules\HR\Recruitment\Support\ApplicationStatus;
use App\Modules\HR\Recruitment\Support\OnboardingCaseStatus;

class RecruitmentDashboardService
{
    public function __construct(
        private readonly RecruitmentDashboardRepository $dashboard,
    ) {}

    /** @return array<string, mixed> */
    public function build(int $tenantId): array
    {
        return [
            'kpis' => $this->kpis($tenantId),
            'pipeline' => $this->dashboard->applicationPipeline($tenantId),
            'upcomingInterviews' => $this->dashboard->upcomingInterviews($tenantId),
            'recentApplications' => $this->dashboard->recentApplications($tenantId),
            'activeOnboarding' => $this->dashboard->activeOnboarding($tenantId),
            'shortcuts' => $this->shortcuts(),
        ];
    }

    /** @return array<string, int|float|string> */
    private function kpis(int $tenantId): array
    {
        $activeApps = $this->dashboard->countApplications($tenantId, [
            ApplicationStatus::REJECTED,
            ApplicationStatus::WITHDRAWN,
            ApplicationStatus::HIRED,
        ]);

        return [
            'open_vacancies' => $this->dashboard->openVacancies($tenantId),
            'active_applications' => $activeApps,
            'scheduled_interviews' => $this->dashboard->scheduledInterviews($tenantId),
            'offers_pending' => $this->dashboard->pendingOffers($tenantId),
            'pending_job_requests' => $this->dashboard->pendingJobRequests($tenantId),
            'hires_this_month' => $this->dashboard->countHiringsThisMonth($tenantId),
            'onboarding_in_progress' => $this->dashboard->countOnboarding(
                $tenantId,
                OnboardingCaseStatus::IN_PROGRESS,
            ),
            'onboarding_completed' => $this->dashboard->countOnboarding(
                $tenantId,
                OnboardingCaseStatus::COMPLETED,
            ),
        ];
    }

    /** @return list<array{route: string, icon: string, label_key: string}> */
    private function shortcuts(): array
    {
        return [
            ['route' => 'hr.recruitment.job-requests.index', 'icon' => 'fe fe-file-plus', 'label_key' => 'job_requests'],
            ['route' => 'hr.recruitment.vacancies.index', 'icon' => 'fe fe-briefcase', 'label_key' => 'vacancies'],
            ['route' => 'hr.recruitment.applications.index', 'icon' => 'fe fe-inbox', 'label_key' => 'applications'],
            ['route' => 'hr.recruitment.candidates.index', 'icon' => 'fe fe-users', 'label_key' => 'candidates'],
            ['route' => 'hr.recruitment.interviews.index', 'icon' => 'fe fe-calendar', 'label_key' => 'interviews'],
            ['route' => 'hr.recruitment.offer-letters.index', 'icon' => 'fe fe-mail', 'label_key' => 'offer_letters'],
            ['route' => 'hr.recruitment.hiring.index', 'icon' => 'fe fe-user-plus', 'label_key' => 'hiring'],
            ['route' => 'hr.recruitment.onboarding.index', 'icon' => 'fe fe-user-check', 'label_key' => 'onboarding'],
            ['route' => 'hr.recruitment.career-portal', 'icon' => 'fe fe-globe', 'label_key' => 'career_portal'],
            ['route' => 'hr.recruitment.reports.index', 'icon' => 'fe fe-bar-chart-2', 'label_key' => 'reports', 'permission' => 'recruitment.reports.view'],
        ];
    }
}