import AppBar from "@/components/AppBar";
import { AuthOptions, getServerSession } from "next-auth";
import { authOptions, SessionWithTokens } from "@/config/authConfig";
import { redirect } from "next/navigation";
import MainContentArea from "@/app/(main)/MainContentArea";
import MainLayoutBody from "@/app/(main)/MainLayoutBody";
import { AxiosProvider } from "@/app/(main)/axios/AxiosProvider";
import { SetupAxiosInterceptors } from "@/app/(main)/axios/axiosInstance";
import { UploadProvider } from "@/components/UploadProvider/UploadProvider";
import SessionProviderWrapper from "@/components/SessionProviderWrapper/SessionProviderWrapper";
import QrCodeScannerProvider from "@/components/QrCodeScannerProvider/QrCodeScannerProvider";
import { I18nProvider } from "@/i18n";
import "src/app/global.css";
export default async function MainLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const session = await getServerSession<AuthOptions, SessionWithTokens>(
    authOptions,
  );

  if (!session?.user) {
    redirect("/login");
  }

  // console.log(session?.user);

  // Verify if the session is valid
  if (session) {
    SetupAxiosInterceptors(session.accessToken);
  }

  return (
    <SessionProviderWrapper session={session}>
      <UploadProvider>
        {/* <CameraProvider> */}
        <AxiosProvider>
          <QrCodeScannerProvider>
            <MainLayoutBody
              appBar={
                <AppBar
                  profileName={session.user.name!}
                  avatarImageSrc={session.user.image || undefined}
                />
              }
              mainContent={
                <I18nProvider namespaces={["common"]}>
                  <MainContentArea>{children}</MainContentArea>
                </I18nProvider>
              }
            />
          </QrCodeScannerProvider>
        </AxiosProvider>
        {/* </CameraProvider> */}
      </UploadProvider>
    </SessionProviderWrapper>
  );
}
