import { ARTICLES } from '@/lib/articles';
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'];
  /** Real last-substantive-change date. Bump when the page meaningfully changes. */
  lastModified: string;
};

// lastModified must reflect actual content changes — Google discounts sitemaps
// whose lastmod is always "now". Bump a route's date when you edit its page.
const ROUTES: Entry[] = [
  { path: '/', priority: 1.0, changeFrequency: 'weekly', lastModified: '2026-07-08' },
  { path: '/pricing', priority: 0.9, changeFrequency: 'weekly', lastModified: '2026-07-08' },
  { path: '/templates', priority: 0.9, changeFrequency: 'daily', lastModified: '2026-07-08' },
  { path: '/guides', priority: 0.8, changeFrequency: 'weekly', lastModified: '2026-07-08' },
  { path: '/docs', priority: 0.8, changeFrequency: 'weekly', lastModified: '2026-07-08' },
  { path: '/docs/concepts', priority: 0.7, changeFrequency: 'monthly', lastModified: '2026-07-08' },
  { path: '/docs/oauth', priority: 0.7, changeFrequency: 'monthly', lastModified: '2026-07-08' },
  { path: '/docs/authoring', priority: 0.7, changeFrequency: 'monthly', lastModified: '2026-07-08' },
  {
    path: '/docs/api-reference',
    priority: 0.7,
    changeFrequency: 'monthly',
    lastModified: '2026-07-08',
  },
  {
    path: '/docs/self-hosting',
    priority: 0.7,
    changeFrequency: 'monthly',
    lastModified: '2026-07-08',
  },
  { path: '/docs/faq', priority: 0.6, changeFrequency: 'monthly', lastModified: '2026-07-08' },
  { path: '/changelog', priority: 0.6, changeFrequency: 'weekly', lastModified: '2026-06-11' },
  { path: '/security', priority: 0.5, changeFrequency: 'monthly', lastModified: '2026-06-04' },
  { path: '/contact', priority: 0.4, changeFrequency: 'yearly', lastModified: '2026-06-04' },
  { path: '/status', priority: 0.4, changeFrequency: 'weekly', lastModified: '2026-06-04' },
  { path: '/privacy', priority: 0.3, changeFrequency: 'yearly', lastModified: '2026-06-29' },
  { path: '/terms', priority: 0.3, changeFrequency: 'yearly', lastModified: '2026-06-29' },
];

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

  // Guide articles come from the single registry, with their true dates.
  const guideEntries: MetadataRoute.Sitemap = ARTICLES.map((a) => ({
    url: `${SITE_URL}/guides/${a.slug}`,
    lastModified: new Date(a.dateModified ?? a.datePublished),
    changeFrequency: 'monthly',
    priority: 0.8,
  }));

  // 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: new Date('2026-07-08'),
    changeFrequency: 'weekly',
    priority: 0.6,
  }));

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