import type { RequestHandler } from './$types';
import { calculators, categories } from '$lib/data/calculators';

export const GET: RequestHandler = async () => {
	const calculatorUrls = calculators.map(
		(calc) => `
  <url>
    <loc>https://howdoyouconvert.com/${calc.slug}</loc>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>`
	);

	const categoryUrls = Object.keys(categories).map(
		(category) => `
  <url>
    <loc>https://howdoyouconvert.com/category/${category}</loc>
    <changefreq>weekly</changefreq>
    <priority>0.9</priority>
  </url>`
	);

	const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://howdoyouconvert.com/</loc>
    <changefreq>weekly</changefreq>
    <priority>1.0</priority>
  </url>
  ${categoryUrls.join('')}
  ${calculatorUrls.join('')}
</urlset>`;

	return new Response(sitemap, {
		headers: {
			'Content-Type': 'application/xml',
			'Cache-Control': 'max-age=0, s-maxage=3600'
		}
	});
};
