import { NextResponse } from "next/server";

export async function GET() {
  try {
    const res = await fetch(
      "https://www.togwe.com/backend/post-sitemap.xml",
      {
        headers: { "User-Agent": "Mozilla/5.0" },
        cache: "no-store",
      }
    );

    if (!res.ok) {
      return new NextResponse("Post sitemap fetch failed", { status: 500 });
    }

    let xml = await res.text();

    // ✅ Fix page/post URLs only
    xml = xml.replace(
      /<loc>https:\/\/www\.togwe\.com\/backend\//g,
      "<loc>https://www.togwe.com/"
    );

    return new NextResponse(xml, {
      headers: { "Content-Type": "application/xml" },
    });
  } catch {
    return new NextResponse("Post sitemap error", { status: 500 });
  }
}
