import type { APIRoute } from 'astro';
import { getArticles, getGenericPageSlugs, getRecipes, getSpices } from '../lib/content';

export const GET: APIRoute = async ({ site }) => {
  const [pages, articles, recipes, spices] = await Promise.all([
    getGenericPageSlugs(), getArticles(), getRecipes(), getSpices(),
  ]);
  const urls = [
    '/', '/articles', '/recipes',
    ...pages.map((s) => `/${s}`),
    ...articles.map((a) => `/articles/${a.slug.current}`),
    ...recipes.map((r) => `/recipes/${r.slug.current}`),
    ...spices.map((s) => `/spices/${s.slug.current}`),
  ];
  const body = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls.map((u) => `  <url><loc>${new URL(u, site)}</loc></url>`).join('\n')}
</urlset>`;
  return new Response(body, { headers: { 'Content-Type': 'application/xml; charset=utf-8' } });
};
