/**
 * Dynamische robots.txt mit Sitemap-URL aus astro.config (site).
 * Siehe: https://docs.astro.build/en/guides/integrations-guide/sitemap/#sitemap-link-in-robots-txt
 * Ausgabe: /robots.txt
 */
import type { APIRoute } from "astro";

export const prerender = true;

const getRobotsTxt = (sitemapURL: URL) =>
  [
    "User-agent: *",
    "Allow: /",
    "",
    `Sitemap: ${sitemapURL.href}`,
  ].join("\n");

export const GET: APIRoute = ({ site }) => {
  const base = site ?? (import.meta.env.SITE as string | undefined) ?? "https://windwiderstand.de";
  const sitemapURL = new URL("sitemap-index.xml", base);
  return new Response(getRobotsTxt(sitemapURL), {
    headers: {
      "Content-Type": "text/plain; charset=utf-8",
      "Cache-Control": "public, max-age=3600",
    },
  });
};
