<?php


namespace AutoAdmin\Bundle\Seo\Controller;


use AutoAdmin\Bundle\Content\Site\SiteContextHelper;
use AutoAdmin\Bundle\Seo\Cache\SitemapCacheHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class SitemapController
{
    public function __construct(
        protected SitemapCacheHandler $cacheHandler,
        protected SiteContextHelper $contextHelper,
    ) {
    }

    public function index(Request $request): Response
    {
        $section = $request->get('section', 'default');

        $sitemaps = $this->cacheHandler->getValue();
        if (!isset($sitemaps[$section])) {
            throw new NotFoundHttpException();
        }

        $sectionSitemap = $sitemaps[$section];

        return new Response($sectionSitemap, 200, ['content-type' => 'text/xml;charset=UTF-8']);
    }
}
