import type { MetadataRoute } from 'next'
import { cms } from '@/lib/site'

export const dynamic = 'force-dynamic'

// Every page in both languages, from the CMS (new courses appear here on their own).
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
  const base = (process.env.SITE_URL || 'http://localhost:8890').replace(/\/$/, '')
  const { docs } = await (await cms()).find({ collection: 'courses', limit: 100, depth: 0, select: { slug: true, updatedAt: true } })
  const paths = ['/', '/courses', '/pricing', '/about', ...docs.map((c) => `/courses/${c.slug}`)]
  return paths.map((p) => ({
    url: `${base}${p === '/' ? '' : p}` || base,
    alternates: { languages: { ar: `${base}${p === '/' ? '' : p}`, en: `${base}/en${p === '/' ? '' : p}` } },
  }))
}
