import { notFound, redirect } from 'next/navigation'
import { getPageSlugById } from '@/lib/db/queries/getPageDetail'
import { getDefaultLanguage } from '@/lib/db/queries/getlanguages'
import { buildLocalizedPath } from '@/lib/i18n/buildLocalizedPath'

interface PageByIdProps {
  params: Promise<{ locale: string; id: string }>
}

// ID-based entry point for CMS pages — `menu_items` rows of type 'page' only
// store the page's id (reference_id), not its slug, so header/footer/mobile
// menu links land here. Resolves the id to that page's slug in the current
// locale and redirects to the real slug-based `/[slug]` route, which owns
// rendering and SEO (so there's still exactly one canonical URL per page).
export default async function PageById({ params }: PageByIdProps) {
  const { locale, id } = await params

  const [slug, defaultLanguage] = await Promise.all([getPageSlugById(id, locale), getDefaultLanguage()])
  if (!slug) notFound()

  redirect(buildLocalizedPath(locale, defaultLanguage?.code ?? 'en', `/${slug}`))
}
