export async function getSitemapRoutes() {
  const CMS_BASE_URL = process.env.NUXT_PUBLIC_CMS_BASE_URL
  const CMS_TOKEN = process.env.NUXT_PUBLIC_CMS_TOKEN

  if (!CMS_BASE_URL || !CMS_TOKEN) {
    throw new Error('CMS-Umgebungsvariablen fehlen')
  }

  const headers = {
    Authorization: `Bearer ${CMS_TOKEN}`,
  }

  try {
    const [projectsRes, articlesRes] = await Promise.all([
      fetch(`${CMS_BASE_URL}/api/references?fields=link`, { headers }).then(res => res.json()),
      fetch(`${CMS_BASE_URL}/api/newsarticels?fields=slug&locale=all`, { headers }).then(res => res.json())
    ])

    const projectRoutes = projectsRes.data
      ?.filter((p: any) => p.attributes.link)
      .map((p: any) => `/projekt/${p.attributes.link}`) || []

    const articleRoutes = articlesRes.data
      ?.filter((a: any) => a.attributes.slug)
      .map((a: any) => `/artikel/${a.attributes.slug}`) || []

    return [...projectRoutes, ...articleRoutes]
  } catch (error) {
    console.error('Fehler beim Abrufen der CMS-Routen für die Sitemap:', error)
    return []
  }
}
