import type { Metadata } from 'next'
import { NextIntlClientProvider } from 'next-intl'
import { getMessages, getTranslations } from 'next-intl/server'
import { notFound } from 'next/navigation'
import Link from 'next/link'
import { locales } from '@/i18n'
import Providers from './Providers'
import Navbar from '@/components/nav/Navbar'

const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || 'https://trumpsignal.com'

interface LayoutProps {
  children: React.ReactNode
  params: Promise<{ locale: string }>
}

export async function generateMetadata({ params }: LayoutProps): Promise<Metadata> {
  const { locale } = await params
  const isZh = false  // i18n shelved — Chinese branches kept as dead code for future revival; see messages/zh.json
  return {
    alternates: {
      canonical: `${siteUrl}/${locale}`,
      languages: {
        'en': `${siteUrl}/en`,
        'x-default': `${siteUrl}/en`,
      },
    },
    openGraph: {
      locale: isZh ? 'zh_CN' : 'en_US',
      alternateLocale: isZh ? ['en_US'] : ['zh_CN'],
    },
  }
}

export default async function LocaleLayout({ children, params }: LayoutProps) {
  const { locale } = await params
  if (!locales.includes(locale as (typeof locales)[number])) {
    notFound()
  }

  const messages = await getMessages()
  const t = await getTranslations({ locale, namespace: 'footer' })
  const href = (path: string) => `/${locale}${path}`

  return (
    <NextIntlClientProvider messages={messages}>
      <Providers>
        <Navbar />
        <main>{children}</main>
        <footer style={{
          borderTop: '1px solid var(--line)',
          padding: '20px 32px',
          display: 'flex',
          gap: 16,
          alignItems: 'center',
          flexWrap: 'wrap',
          fontSize: 12,
          color: 'var(--ink-3)',
          marginTop: 48,
        }}>
          <span>© {new Date().getFullYear()} Trump Alpha — a research project by Endorphin</span>
          <Link href={href('/methodology')} style={{ color: 'var(--ink-3)', textDecoration: 'none' }}>{t('methodology')}</Link>
          <Link href={href('/glossary')} style={{ color: 'var(--ink-3)', textDecoration: 'none' }}>{t('glossary')}</Link>
          <Link href={href('/case-studies')} style={{ color: 'var(--ink-3)', textDecoration: 'none' }}>{t('caseStudies')}</Link>
          <Link href={href('/privacy')} style={{ color: 'var(--ink-3)', textDecoration: 'none' }}>{t('privacy')}</Link>
          <Link href={href('/terms')} style={{ color: 'var(--ink-3)', textDecoration: 'none' }}>{t('terms')}</Link>
          <Link href={href('/contact')} style={{ color: 'var(--ink-3)', textDecoration: 'none' }}>{t('contact')}</Link>
        </footer>
      </Providers>
    </NextIntlClientProvider>
  )
}
