<?php

declare(strict_types=1);

namespace App\Business\DataImport;

use App\Business\DataImport\Enums\DataImportType;
use App\Business\DataImport\Exports\DataImportTemplateExport;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

final class DataImportTemplateDownloadService
{
    public function __construct(
        private readonly DataImportTemplateRegistry $templateRegistry,
    ) {}

    public function download(DataImportType $type): BinaryFileResponse
    {
        if (! $this->templateRegistry->supports($type)) {
            abort(404);
        }

        $filename = $this->templateRegistry->downloadFilename($type);

        return Excel::download(new DataImportTemplateExport($type), $filename);
    }
}
