'use client'

import { useEffect, useState } from 'react'
import { NextIntlClientProvider } from 'next-intl'
import Header from '@/components/Header'
import Footer from '@/components/Footer'
import WhatsAppButton from '@/components/WhatsAppButton'
import NewsDetailClient from './[locale]/news/[slug]/client'
import EventGalleryDetailClient from './[locale]/event-gallery/[slug]/client'

import enMessages from '../../messages/en.json'
import zhMessages from '../../messages/zh.json'
import scMessages from '../../messages/sc.json'

const allMessages: Record<string, Record<string, unknown>> = {
  en: enMessages,
  zh: zhMessages,
  sc: scMessages,
}

const LOCALES = ['en', 'zh', 'sc']
const BASE_PATH = process.env.NEXT_PUBLIC_BASE_PATH || ''

type RouteMatch =
  | { type: 'news'; locale: string; slug: string }
  | { type: 'event-gallery'; locale: string; slug: string }
  | { type: '404'; locale: string }

function matchRoute(pathname: string): RouteMatch {
  const path = pathname.replace(BASE_PATH, '')
  const localePattern = `(${LOCALES.join('|')})`

  const newsMatch = path.match(new RegExp(`^/${localePattern}/news/([^/]+)/?$`))
  if (newsMatch) return { type: 'news', locale: newsMatch[1], slug: newsMatch[2] }

  const galleryMatch = path.match(new RegExp(`^/${localePattern}/event-gallery/([^/]+)/?$`))
  if (galleryMatch) return { type: 'event-gallery', locale: galleryMatch[1], slug: galleryMatch[2] }

  const localeMatch = path.match(new RegExp(`^/${localePattern}`))
  return { type: '404', locale: localeMatch?.[1] || 'en' }
}

export default function NotFound() {
  const [route, setRoute] = useState<RouteMatch | null>(null)

  useEffect(() => {
    setRoute(matchRoute(window.location.pathname))
  }, [])

  if (!route) {
    return (
      <html lang="en" className="scroll-smooth">
        <body className="min-h-screen flex flex-col">
          <div className="min-h-[60vh] flex items-center justify-center">
            <div className="w-8 h-8 border-4 border-primary-200 border-t-primary-600 rounded-full animate-spin" />
          </div>
        </body>
      </html>
    )
  }

  const { locale } = route
  const messages = allMessages[locale] || allMessages.en
  const htmlLang = locale === 'sc' ? 'zh-Hans' : locale === 'zh' ? 'zh-Hant' : 'en'

  let content: React.ReactNode
  switch (route.type) {
    case 'news':
      content = <NewsDetailClient slug={route.slug} />
      break
    case 'event-gallery':
      content = <EventGalleryDetailClient slug={route.slug} />
      break
    default:
      content = (
        <div className="min-h-[60vh] flex flex-col items-center justify-center gap-4 text-center">
          <h1 className="text-4xl font-bold text-secondary-900">404</h1>
          <p className="text-secondary-500 text-lg">Page not found</p>
          <a
            href={`${BASE_PATH}/${locale}`}
            className="text-primary-600 font-semibold hover:underline"
          >
            Go to homepage
          </a>
        </div>
      )
  }

  return (
    <html lang={htmlLang} className="scroll-smooth" suppressHydrationWarning>
      <body className="min-h-screen flex flex-col" suppressHydrationWarning>
        <NextIntlClientProvider locale={locale} messages={messages}>
          <Header />
          <main className="flex-grow">
            {content}
          </main>
          <Footer />
          <WhatsAppButton />
        </NextIntlClientProvider>
      </body>
    </html>
  )
}
