import { NextResponse } from "next/server";
import { generateSitemapXml, getSitemapEntries } from "@/lib/sitemap";

export const dynamic = "force-dynamic";
export const runtime = "nodejs";

export async function GET() {
  try {
    const entries = await getSitemapEntries();
    const xml = generateSitemapXml(entries);
    return new NextResponse(xml, {
      headers: { "Content-Type": "application/xml" },
    });
  } catch (error) {
    console.error("Failed to generate sitemap:", error);
    // Fail closed: return minimal sitemap
    const xml = generateSitemapXml([]);
    return new NextResponse(xml, {
      status: 500,
      headers: { "Content-Type": "application/xml" },
    });
  }
}
