<?php

namespace App\Controller;

use App\Entity\News;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

final class SitemapController extends AbstractController
{
    #[Route('/sitemap.xml', name: 'sitemap', defaults: ['_format' => 'xml'])]
    public function sitemap(EntityManagerInterface $entityManager): Response
    {
        // Routes statiques
        $urls = [
            [
                'loc' => $this->generateUrl('home'),
                'priority' => '1.0',
            ],
            [
                'loc' => $this->generateUrl('about'),
                'priority' => '0.8',
            ],
            [
                'loc' => $this->generateUrl('project'),
                'priority' => '0.8',
            ],
            [
                'loc' => $this->generateUrl('manualproject'),
                'priority' => '0.8',
            ],
            [
                'loc' => $this->generateUrl('show'),
                'priority' => '0.7',
            ],
        ];

        // Routes dynamiques (actualités)
        $newsList = $entityManager->getRepository(News::class)->findAll();

        foreach ($newsList as $news) {
            $urls[] = [
                'loc' => $this->generateUrl('actualites', [
                    'slug' => $news->getSlug()
                ]),
                'priority' => '0.6',
            ];
        }

        return $this->render('sitemap/sitemap.xml.twig', [
            'urls' => $urls,
        ]);
    }
}
