import type { RequestHandler } from './$types';
import { locationPages } from '$lib/content/locations';

const siteUrl = 'https://www.goodwalk.co.nz';

interface SitemapRoute {
  path: string;
  priority: string;
  changefreq: string;
}

const routes: SitemapRoute[] = [
  { path: '/', priority: '1.0', changefreq: 'weekly' },
  { path: '/pack-walks', priority: '0.9', changefreq: 'monthly' },
  { path: '/dog-walking', priority: '0.9', changefreq: 'monthly' },
  { path: '/puppy-visits', priority: '0.9', changefreq: 'monthly' },
  { path: '/our-pricing', priority: '0.8', changefreq: 'monthly' },
  { path: '/about', priority: '0.7', changefreq: 'monthly' },
  { path: '/contact-us', priority: '0.7', changefreq: 'monthly' },
  { path: '/terms-and-conditions', priority: '0.3', changefreq: 'yearly' },
  { path: '/privacy-policy', priority: '0.3', changefreq: 'yearly' }
];

const locationRoutes: SitemapRoute[] = locationPages.map((loc) => ({
  path: `/locations/${loc.slug}`,
  priority: '0.8',
  changefreq: 'monthly'
}));

export const GET: RequestHandler = () => {
  const allRoutes = [...routes, ...locationRoutes];
  const body = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${allRoutes
  .map(
    ({ path, priority, changefreq }) => `  <url>
    <loc>${siteUrl}${path}</loc>
    <changefreq>${changefreq}</changefreq>
    <priority>${priority}</priority>
  </url>`
  )
  .join('\n')}
</urlset>`;

  return new Response(body, {
    headers: {
      'Content-Type': 'application/xml; charset=utf-8'
    }
  });
};
