<?php
declare(strict_types=1);

namespace App\Message\Handler;

use App\Message\Command\Export;
use App\Repository\Access\AccessRepository;
use App\Service\MercureHub;
use App\Service\Notifier;
use App\Service\ServiceIterator\ExporterIterator;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler(priority: 1)]
class ExportHandler
{
    public function __construct(
        private ExporterIterator $handler,
        private MercureHub $mercureHub,
        private AccessRepository $accessRepository,
        private Notifier $notifier
    ) {}

    public function __invoke(Export $export): void
    {
        $exportRequest = $export->getExportRequest();
        $exportService = $this->handler->getExporterFor($exportRequest);
        $file = $exportService->export($exportRequest);

        $this->mercureHub->publishUpdate($exportRequest->getRequesterId(), $file);

        $this->notifier->notifyExport(
            $this->accessRepository->find($exportRequest->getRequesterId()),
            $file
        );
    }


}
