import { MetadataRoute } from 'next'
import { routing } from '@/i18n/routing'

const BASE_URL = process.env.NEXT_PUBLIC_APP_URL ?? 'https://www.cahootravel.com'

const PUBLIC_PAGES: { path: string; priority: number; changeFrequency: MetadataRoute.Sitemap[number]['changeFrequency'] }[] = [
  { path: '',                priority: 1.0, changeFrequency: 'daily' },
  { path: '/how-it-works',  priority: 0.9, changeFrequency: 'weekly' },
  { path: '/pricing',       priority: 0.9, changeFrequency: 'weekly' },
  { path: '/about',         priority: 0.8, changeFrequency: 'monthly' },
  { path: '/contact',       priority: 0.7, changeFrequency: 'monthly' },
  { path: '/partners',      priority: 0.7, changeFrequency: 'monthly' },
  { path: '/platforms',     priority: 0.7, changeFrequency: 'weekly' },
  { path: '/countries',     priority: 0.7, changeFrequency: 'weekly' },
  { path: '/support',       priority: 0.6, changeFrequency: 'monthly' },
  { path: '/security',      priority: 0.5, changeFrequency: 'monthly' },
  { path: '/privacy',       priority: 0.5, changeFrequency: 'monthly' },
  { path: '/refund-policy', priority: 0.5, changeFrequency: 'monthly' },
  { path: '/terms',         priority: 0.5, changeFrequency: 'monthly' },
  { path: '/cookies',       priority: 0.4, changeFrequency: 'monthly' },
]

function localeSegment(locale: string): string {
  return locale === routing.defaultLocale ? '' : `/${locale}`
}

export default function sitemap(): MetadataRoute.Sitemap {
  const entries: MetadataRoute.Sitemap = []

  for (const page of PUBLIC_PAGES) {
    for (const locale of routing.locales) {
      const localePath = localeSegment(locale)
      entries.push({
        url: `${BASE_URL}${localePath}${page.path}`,
        lastModified: new Date(),
        changeFrequency: page.changeFrequency,
        priority: page.priority,
        alternates: {
          languages: Object.fromEntries(
            routing.locales.map(l => [l, `${BASE_URL}${localeSegment(l)}${page.path}`])
          ),
        },
      })
    }
  }

  return entries
}
