import type { Metadata } from 'next'
import { Inter, JetBrains_Mono, VT323 } from 'next/font/google'
import { notFound } from 'next/navigation'
import { NextIntlClientProvider } from 'next-intl'
import { getMessages } from 'next-intl/server'
import { routing, type Locale } from '@/i18n/routing'
import '../globals.css'
import { Providers } from '../providers'
import { AutoHealingErrorBoundary } from '@/components/shared/auto-healing-error-boundary'

const inter = Inter({
  subsets: ['latin'],
  variable: '--font-inter',
})

const jetbrainsMono = JetBrains_Mono({
  subsets: ['latin'],
  variable: '--font-mono',
})

// VT323 點陣字體 - 品牌與 AI 狀態專用
const vt323 = VT323({
  weight: '400',
  subsets: ['latin'],
  variable: '--font-dot-matrix',
})

export function generateStaticParams() {
  return routing.locales.map((locale) => ({ locale }))
}

export async function generateMetadata({
  params: { locale: _locale },
}: {
  params: { locale: Locale }
}): Promise<Metadata> {
  const messages = await getMessages()
  const metadata = messages.metadata as { title: string; description: string }

  return {
    title: metadata?.title || 'AWOOOI',
    description: metadata?.description || 'AI-Powered Intelligent Operations Platform',
  }
}

export default async function LocaleLayout({
  children,
  params: { locale },
}: {
  children: React.ReactNode
  params: { locale: string }
}) {
  // 驗證語系
  if (!routing.locales.includes(locale as Locale)) {
    notFound()
  }

  // 取得翻譯訊息
  const messages = await getMessages()

  return (
    <html lang={locale}>
      <body
        className={`${inter.variable} ${jetbrainsMono.variable} ${vt323.variable} font-body bg-nothing-gray-50 text-nothing-black antialiased`}
      >
        <NextIntlClientProvider messages={messages}>
          <AutoHealingErrorBoundary fallbackMessage="Critical Application Error">
            <Providers>{children}</Providers>
          </AutoHealingErrorBoundary>
        </NextIntlClientProvider>
      </body>
    </html>
  )
}
