<?php declare(strict_types=1);

namespace App\Engine\Page;

final class PageSitemapSubscriber extends \App\Sitemap\SitemapSubscriber
{

    public function __construct(
        private readonly PageRepository $pageRepository,
    ) {
    }

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

        foreach ($pages as $page) {
            $url = $router->generate('PageDetail', [
                'url' => ltrim($page->getUrl(), '/'),
            ], \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,
            );
        }
    }

}