import { MetadataRoute } from 'next';
import { getAllDocPages, getAllFaqPages } from '@/lib/content/loader';
import { seoKeywordEntries } from '@/lib/seo/keywords';
import { setupToolEntries } from '@/lib/seo/setup-tools';

export const dynamic = 'force-static';

export default function sitemap(): MetadataRoute.Sitemap {
  const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'https://ai-devkit.com';

  const staticRoutes = [
    {
      url: baseUrl,
      lastModified: new Date(),
      changeFrequency: 'weekly' as const,
      priority: 1.0,
    },
    {
      url: `${baseUrl}/faq`,
      lastModified: new Date(),
      changeFrequency: 'weekly' as const,
      priority: 0.8,
    },
    {
      url: `${baseUrl}/docs`,
      lastModified: new Date(),
      changeFrequency: 'weekly' as const,
      priority: 0.9,
    },
    {
      url: `${baseUrl}/vision`,
      lastModified: new Date(),
      changeFrequency: 'monthly' as const,
      priority: 0.8,
    },
    {
      url: `${baseUrl}/roadmap`,
      lastModified: new Date(),
      changeFrequency: 'weekly' as const,
      priority: 0.8,
    },
    {
      url: `${baseUrl}/skills`,
      lastModified: new Date(),
      changeFrequency: 'weekly' as const,
      priority: 0.8,
    },
  ];

  const docs = getAllDocPages();
  const docRoutes = docs.map((doc) => ({
    url: `${baseUrl}/docs/${doc.metadata.slug}`,
    lastModified: new Date(),
    changeFrequency: 'weekly' as const,
    priority: 0.7,
  }));

  const faqs = getAllFaqPages();
  const faqRoutes = faqs.map((faq) => ({
    url: `${baseUrl}/faq/${faq.metadata.slug}`,
    lastModified: new Date(),
    changeFrequency: 'weekly' as const,
    priority: 0.7,
  }));

  const seoRoutes = seoKeywordEntries.map((entry) => ({
    url: `${baseUrl}/faq/${entry.slug}`,
    lastModified: new Date(),
    changeFrequency: 'weekly' as const,
    priority: 0.6,
  }));

  const setupRoutes = setupToolEntries.map((entry) => ({
    url: `${baseUrl}/faq/getting-started/${entry.slug}`,
    lastModified: new Date(),
    changeFrequency: 'weekly' as const,
    priority: 0.6,
  }));

  const allRoutes = [...staticRoutes, ...docRoutes, ...faqRoutes, ...seoRoutes, ...setupRoutes];
  const routeMap = new Map(allRoutes.map((route) => [route.url, route]));

  return Array.from(routeMap.values());
}
