<?php

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

use App\Modules\HR\Recruitment\DTOs\RecruitmentReportFilters;
use App\Modules\HR\Recruitment\Repositories\RecruitmentReportRepository;

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

    public function build(int $tenantId, RecruitmentReportFilters $filters): array
    {
        $rows = [];
        foreach ($this->reports->hiringsBase($tenantId, $filters)->orderByDesc('hired_at')->limit(500)->get() as $h) {
            $rows[] = [
                $h->reference_code,
                $h->employee?->employee_code,
                $h->employee?->full_name,
                $h->candidate?->full_name,
                $h->vacancy?->title,
                $h->offerLetter?->reference_code,
                $h->hired_at?->format('Y-m-d H:i'),
            ];
        }

        return [
            'title_key' => 'recruitment.reports.types.hiring',
            'columns' => ['reference', 'employee_code', 'employee', 'candidate', 'vacancy', 'offer', 'hired_at'],
            'rows' => $rows,
            'summary' => ['hires' => count($rows)],
        ];
    }
}
