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

export const dynamic = "force-dynamic";

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("Error generating sitemap.xml:", error);
    // Always return a valid sitemap with 200 so crawlers don't treat it as broken.
    return new NextResponse(generateSitemapXml([]), {
      headers: { "Content-Type": "application/xml" },
    });
  }
}
