import "@/styles/reset.css";
import "@/styles/globals.css";
import "react-circular-progressbar/dist/styles.css";

import type { Metadata, Viewport } from "next";
import { Inter } from "next/font/google";
import { notFound } from "next/navigation";
import { hasLocale, NextIntlClientProvider } from "next-intl";
import { getMessages } from "next-intl/server";
import clsx from "clsx";

import { PageViewTracker,YandexMetrika } from "@/components/analytics";
import { loadChatsList } from "@/entities/chats/loaders";
import { loadUser, loadUserId } from "@/entities/user/loaders";
import { routing } from "@/i18n/routing";
import { AnalyticsProvider } from "@/providers/analytics-provider";
import { AppUiStoreProvider } from "@/providers/app-ui-store-provider";
import { AudioProvider } from "@/providers/audio-provider";
import { ChatsInitializationProvider } from "@/providers/chats-initialization-provider";
import { ChatsProvider } from "@/providers/chats-provider";
import { RetainingStoreProvider } from "@/providers/retaining-store-provider";
import SocketProvider from "@/providers/socket-provider";
import { ToastProvider } from "@/providers/toast-provider";
import { UserProvider } from "@/providers/user-provider";

import styles from "./layout.module.scss";

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

const inter = Inter({
  subsets: ["latin", "cyrillic"],
  display: "swap",
  variable: "--font-inter",
});

export const viewport: Viewport = {
  width: "device-width",
  initialScale: 1,
  maximumScale: 1,
  userScalable: false,
};

export const metadata: Metadata = {
  title: "WIT",
  description:
    "More than 14M people have experienced the value of our products. Wit Apps, headquartered in Silicon Valley, California, is a tech company that constructs global enterprises specializing in mobile-first products. We believe in the transformative power of technology, capable of turning chaos into miracles, thus enhancing the lives of millions. To realize this vision, we integrate leading expertise in artificial intelligence with a deep understanding of consumer needs and lifestyle trends.",
};

export default async function RootLayout({
  children,
  params,
}: Readonly<{
  children: React.ReactNode;
  params: Promise<{ locale: string }>;
}>) {
  const { locale } = await params;
  if (!hasLocale(routing.locales, locale)) {
    notFound();
  }

  const messages = await getMessages();

  const user = await loadUser();
  const userId = await loadUserId();
  const chats = await loadChatsList();

  return (
    <html lang={locale}>
      <body className={clsx(inter.variable, styles.body)}>
        {/* Analytics Components */}
        <YandexMetrika />

        <AnalyticsProvider>
          <NextIntlClientProvider messages={messages}>
            <UserProvider user={user}>
              <SocketProvider userId={userId}>
                <RetainingStoreProvider>
                  <AudioProvider>
                    <ChatsInitializationProvider>
                      <ChatsProvider initialChats={chats}>
                        <ToastProvider maxVisible={3}>
                          <PageViewTracker />
                          <AppUiStoreProvider>{children}</AppUiStoreProvider>
                        </ToastProvider>
                      </ChatsProvider>
                    </ChatsInitializationProvider>
                  </AudioProvider>
                </RetainingStoreProvider>
              </SocketProvider>
            </UserProvider>
          </NextIntlClientProvider>
        </AnalyticsProvider>
      </body>
    </html>
  );
}
