import { SITE_URL } from '@/lib/seo';
import { fetchPublicTemplateSlugs } from '@/lib/templates-server';
import type { MetadataRoute } from 'next';

type Entry = {
  path: string;
  priority: number;
  changeFrequency: MetadataRoute.Sitemap[number]['changeFrequency'];
};

const ROUTES: Entry[] = [
  { path: '/', priority: 1.0, changeFrequency: 'weekly' },
  { path: '/pricing', priority: 0.9, changeFrequency: 'weekly' },
  { path: '/templates', priority: 0.9, changeFrequency: 'daily' },
  { path: '/guides', priority: 0.8, changeFrequency: 'weekly' },
  { path: '/guides/host-mcp-server-with-oauth', priority: 0.8, changeFrequency: 'monthly' },
  { path: '/guides/hosted-mcp-platforms-compared', priority: 0.8, changeFrequency: 'monthly' },
  { path: '/guides/mintmcp-alternative', priority: 0.7, changeFrequency: 'monthly' },
  { path: '/docs', priority: 0.8, changeFrequency: 'weekly' },
  { path: '/docs/concepts', priority: 0.7, changeFrequency: 'monthly' },
  { path: '/docs/oauth', priority: 0.7, changeFrequency: 'monthly' },
  { path: '/docs/authoring', priority: 0.7, changeFrequency: 'monthly' },
  { path: '/docs/api-reference', priority: 0.7, changeFrequency: 'monthly' },
  { path: '/docs/self-hosting', priority: 0.7, changeFrequency: 'monthly' },
  { path: '/docs/faq', priority: 0.6, changeFrequency: 'monthly' },
  { path: '/changelog', priority: 0.6, changeFrequency: 'weekly' },
  { path: '/security', priority: 0.5, changeFrequency: 'monthly' },
  { path: '/status', priority: 0.4, changeFrequency: 'weekly' },
  { path: '/privacy', priority: 0.3, changeFrequency: 'yearly' },
  { path: '/terms', priority: 0.3, changeFrequency: 'yearly' },
];

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
  const now = new Date();
  const staticEntries: MetadataRoute.Sitemap = ROUTES.map((r) => ({
    url: `${SITE_URL}${r.path}`,
    lastModified: now,
    changeFrequency: r.changeFrequency,
    priority: r.priority,
  }));

  // Marketplace templates — each public template is its own indexable page.
  // Best-effort: if the API is unreachable the static entries still ship.
  const slugs = await fetchPublicTemplateSlugs();
  const templateEntries: MetadataRoute.Sitemap = slugs.map((slug) => ({
    url: `${SITE_URL}/templates/${slug}`,
    lastModified: now,
    changeFrequency: 'weekly',
    priority: 0.6,
  }));

  return [...staticEntries, ...templateEntries];
}
