import { MetadataRoute } from 'next'
import { prisma } from '@/lib/prisma'

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
  const baseUrl = 'https://ayrisapart.com'
  const languages = ['tr', 'en']
  
  // 1. Static Routes
  const staticRoutes = [
    '',
    '/about',
    '/suites',
    '/blog',
    '/contact',
  ]

  const staticEntries: MetadataRoute.Sitemap = []
  
  languages.forEach((lang) => {
    staticRoutes.forEach((route) => {
      staticEntries.push({
        url: `${baseUrl}/${lang}${route}`,
        lastModified: new Date(),
        changeFrequency: 'weekly',
        priority: route === '' ? 1 : 0.8,
      })
    })
  })

  // 2. Dynamic Suite Routes (8 mythological rooms)
  const suites = ['iris', 'electra', 'arke', 'harpy', 'hydaspes', 'zephyrus', 'pothos', 'thaumas']
  const suiteEntries: MetadataRoute.Sitemap = []
  
  languages.forEach((lang) => {
    suites.forEach((id) => {
      suiteEntries.push({
        url: `${baseUrl}/${lang}/suites/${id}`,
        lastModified: new Date(),
        changeFrequency: 'monthly',
        priority: 0.7,
      })
    })
  })

  // 3. Dynamic News Routes (from Database)
  const posts = await prisma.post.findMany({
    where: { published: true },
    select: { slug: true, lang: true, updatedAt: true }
  })

  const newsEntries: MetadataRoute.Sitemap = posts.map((post) => ({
    url: `${baseUrl}/${post.lang}/blog/${post.slug}`,
    lastModified: post.updatedAt,
    changeFrequency: 'monthly',
    priority: 0.6,
  }))

  return [...staticEntries, ...suiteEntries, ...newsEntries]
}
