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;
  lastmod: string;
}

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

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

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, lastmod }) => `  <url>
    <loc>${siteUrl}${path}</loc>
    <lastmod>${lastmod}</lastmod>
    <changefreq>${changefreq}</changefreq>
    <priority>${priority}</priority>
  </url>`
  )
  .join('\n')}
</urlset>`;

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