<?php declare(strict_types=1);

namespace App\Engine\UpcomingEvent;

final class UpcomingEventSitemapSubscriber extends \App\Sitemap\SitemapSubscriber
{

    public function __construct(
        private readonly UpcomingEventRepository $upcomingEventRepository,
    ) {
    }

    public function registerUrls(\Presta\SitemapBundle\Service\UrlContainerInterface $urls, \Symfony\Component\Routing\Generator\UrlGeneratorInterface $router): void
    {
        $upcomingEvents = $this->upcomingEventRepository->findAll();

        foreach ($upcomingEvents as $upcomingEvent) {
            $url = $router->generate('UpcomingEventDetail', [
                'urlPart' => ltrim($upcomingEvent->getUrlPart(), '/'),
            ], \Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_URL);

            $urls->addUrl(
                new \Presta\SitemapBundle\Sitemap\Url\UrlConcrete($url)
                    ->setChangefreq(\Presta\SitemapBundle\Sitemap\Url\UrlConcrete::CHANGEFREQ_DAILY)
                    ->setPriority(1),
                self::DEFAULT_SECTION,
            );
        }
    }

}