import { NextResponse } from 'next/server';

export async function GET() {
  try {
    const response = await fetch(`${process.env.API_BASE_URL}common/sitemap`);
    const data = await response.json();

    let sitemap = `<?xml version="1.0" encoding="UTF-8"?>`;
    sitemap += `<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`;

    // data?.forEach((page: any) => {
    //   sitemap += `
    //     <sitemap>
    //       <loc>${page.url}</loc>
    //       <lastmod>${page.lastmod}</lastmod>
    //       <changefreq>${page.changefreq}</changefreq>
    //       <priority>${page.priority}</priority>
    //     </sitemap>
    //   `;
    // });

    data?.forEach((page: any) => {
      sitemap += `
        <sitemap>
          <loc>${page.url}</loc>
          <lastmod>${page.lastmod}</lastmod>
        </sitemap>
      `;
    });

    sitemap += `</sitemapindex>`;

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