<?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\ApplicationStatus;

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

    public function build(int $tenantId, RecruitmentReportFilters $filters): array
    {
        $base = $this->reports->applicationsBase($tenantId, new RecruitmentReportFilters(
            dateFrom: $filters->dateFrom,
            dateTo: $filters->dateTo,
            departmentId: $filters->departmentId,
            vacancyId: $filters->vacancyId,
            recruiterId: $filters->recruiterId,
            status: null,
        ));

        $rows = [];
        $summary = [];
        foreach (ApplicationStatus::all() as $status) {
            if ($filters->status !== null && $filters->status !== $status) {
                continue;
            }
            $count = (clone $base)->where('status', $status)->count();
            $summary[$status] = $count;
            $rows[] = [__('recruitment.applications.status.'.$status), $count];
        }

        return [
            'title_key' => 'recruitment.reports.types.candidate_pipeline',
            'columns' => ['status', 'count'],
            'rows' => $rows,
            'summary' => $summary,
        ];
    }
}
