<?php

namespace App\Modules\HR\Recruitment\Services\ReportBuilders;

use App\Modules\HR\Recruitment\DTOs\RecruitmentReportFilters;
use App\Modules\HR\Recruitment\Repositories\RecruitmentReportRepository;
use App\Modules\HR\Recruitment\Support\InterviewStatus;
use App\Modules\HR\Recruitment\Support\ScorecardRecommendation;

class InterviewResultsReportBuilder implements RecruitmentReportBuilder
{
    public function __construct(
        private readonly RecruitmentReportRepository $reports,
    ) {}

    public function build(int $tenantId, RecruitmentReportFilters $filters): array
    {
        $byStatus = [];
        foreach (InterviewStatus::all() as $status) {
            $byStatus[$status] = $this->reports->interviewsBase(
                $tenantId,
                new RecruitmentReportFilters(
                    dateFrom: $filters->dateFrom,
                    dateTo: $filters->dateTo,
                    departmentId: $filters->departmentId,
                    vacancyId: $filters->vacancyId,
                    recruiterId: $filters->recruiterId,
                    status: $status,
                )
            )->count();
        }

        $byRec = [];
        foreach (ScorecardRecommendation::all() as $rec) {
            $byRec[$rec] = $this->reports->scorecardsBase(
                $tenantId,
                new RecruitmentReportFilters(
                    dateFrom: $filters->dateFrom,
                    dateTo: $filters->dateTo,
                    departmentId: $filters->departmentId,
                    vacancyId: $filters->vacancyId,
                    recruiterId: $filters->recruiterId,
                    status: $rec,
                )
            )->count();
        }

        $rows = [];
        foreach ($byStatus as $status => $count) {
            $rows[] = [__('recruitment.interviews.status.'.$status), $count, 'status'];
        }
        foreach ($byRec as $rec => $count) {
            $rows[] = [__('recruitment.interview_scorecards.recommendation.'.$rec), $count, 'recommendation'];
        }

        return [
            'title_key' => 'recruitment.reports.types.interview_results',
            'columns' => ['label', 'count', 'group'],
            'rows' => $rows,
            'summary' => array_merge($byStatus, $byRec),
        ];
    }
}
