import { NextResponse } from "next/server";
import { buildShortsSitemap } from "@/lib/seoGenerator";
import { getCached, setCached } from "@/lib/memoryCache";

export const dynamic = "force-dynamic";

export async function GET() {
  const cacheKey = "sitemap_shorts_xml_v2";
  let xml = getCached<string>(cacheKey);

  if (!xml) {
    xml = await buildShortsSitemap();
    setCached(cacheKey, xml, 86400000); // 24h
  }

  return new NextResponse(xml, {
    headers: {
      "Content-Type": "application/xml; charset=utf-8",
      "Cache-Control": "public, max-age=86400, stale-while-revalidate=604800",
    },
  });
}
