import { APP_NAME } from '@/constants/system'
import { routing } from '@/i18n/routing'
import { AuthProvider } from '@/providers/auth-provider'
import LocaleWatcher from '@/providers/locale-provider'
import ReactQueryProvider from '@/providers/query-client-provider'
import { Toaster } from '@/share/ui/sonner'
import { TooltipProvider } from '@/share/ui/tooltip'
import dayjs from 'dayjs'
import updateLocale from 'dayjs/plugin/updateLocale'
import { Metadata } from 'next'
import { hasLocale, NextIntlClientProvider } from 'next-intl'
import { setRequestLocale } from 'next-intl/server'
import { notFound } from 'next/navigation'

dayjs.extend(updateLocale)

export const metadata: Metadata = {
  title: {
    template: '%s | ' + APP_NAME,
    default: APP_NAME,
  },
  description: `Generated by ${APP_NAME}`,
}

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

export default async function LocaleLayout({
  children,
  params,
}: LayoutProps<'/[locale]'>) {
  const { locale } = await params
  if (!hasLocale(routing.locales, locale)) {
    notFound()
  }

  setRequestLocale(locale)

  return (
    <html lang={locale}>
      <body className={`antialiased`}>
        <LocaleWatcher locale={locale} />
        <NextIntlClientProvider>
          <ReactQueryProvider>
            <TooltipProvider>
              <AuthProvider>{children}</AuthProvider>
            </TooltipProvider>
          </ReactQueryProvider>
          <Toaster />
        </NextIntlClientProvider>
      </body>
    </html>
  )
}
