<?php

declare(strict_types=1);

namespace App\Services\Export;

use App\Models\Employee;
use App\Services\Employee\EmployeeStatisticsService;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Http\Response;
use Illuminate\Support\Collection;

final class PdfExportService
{
    public function __construct(
        private readonly EmployeeStatisticsService $statisticsService,
    ) {}

    public function employeeProfile(Employee $employee): Response
    {
        $employee->load(['department', 'position', 'shift']);
        $summary = $this->statisticsService->summary($employee);

        return Pdf::loadView('exports.employee-profile', [
            'employee' => $employee,
            'summary' => $summary,
        ])->download("employee-{$employee->employee_number}.pdf");
    }

    /**
     * @param  Collection<int, Employee>  $employees
     */
    public function employeeList(Collection $employees): Response
    {
        $employees->load(['department', 'position', 'shift']);

        return Pdf::loadView('exports.employee-list', [
            'employees' => $employees,
            'generatedAt' => now(),
        ])->download('employees-'.now()->format('Y-m-d').'.pdf');
    }

    public function leaveSummary(Employee $employee): Response
    {
        $employee->load([
            'vacations' => fn ($query) => $query->latest('start_date')->limit(20),
            'sickLeaves' => fn ($query) => $query->latest('start_date')->limit(20),
            'unpaidLeaves' => fn ($query) => $query->latest('start_date')->limit(20),
        ]);

        $summary = $this->statisticsService->summary($employee);

        return Pdf::loadView('exports.leave-summary', [
            'employee' => $employee,
            'summary' => $summary,
        ])->download("leave-summary-{$employee->employee_number}.pdf");
    }
}
