import type { APIRoute } from 'astro';
import { CANONICAL_URL, PUBLIC_SITE_ROOT_URL, SITE_IS_SUBPATH_BUILD } from '../lib/site';

export const prerender = false;

export const GET: APIRoute = () => {
  const sitemapRootUrl = process.env.PUBLIC_SITE_URL || CANONICAL_URL;
  const pages = [
    { path: '', priority: '1.0', changefreq: 'weekly' },
    { path: 'formation/', priority: '0.9', changefreq: 'monthly' },
    { path: 'preview/', priority: '0.8', changefreq: 'weekly' },
    { path: 'mentions-legales/', priority: '0.3', changefreq: 'yearly' }
  ];

  const urls = pages
    .map(
      (page) => `  <url>
    <loc>${new URL(page.path, sitemapRootUrl).href}</loc>
    <lastmod>${new Date().toISOString().split('T')[0]}</lastmod>
    <changefreq>${page.changefreq}</changefreq>
    <priority>${page.priority}</priority>
  </url>`
    )
    .join('\n');

  const body = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls}
</urlset>
`;

  return new Response(body, {
    headers: { 'Content-Type': 'application/xml; charset=utf-8' }
  });
};
