import AppBar from "@/components/AppBar";
import AutoLogoutProvider from "@/components/AutoLogoutProvider";
import { getServerSession } from "next-auth";
import { authOptions } from "@/config/authConfig";
import { redirect } from "next/navigation";
import Box from "@mui/material/Box";
import { NAVIGATION_CONTENT_WIDTH } from "@/config/uiConfig";
import Stack from "@mui/material/Stack";
import Breadcrumb from "@/components/Breadcrumb";
import { I18nProvider } from "@/i18n";
import RefreshTokenProvider from "@/components/RefreshTokenProvider";
import SessionProvider from "@/components/SessionProvider"

export default async function MainLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const session = await getServerSession(authOptions);

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

  return (
    <>
      <AppBar
        profileName={session.user.name!}
        avatarImageSrc={session.user.image || undefined}
      />
      <Box
        component="main"
        sx={{
          marginInlineStart: { xs: 0, xl: NAVIGATION_CONTENT_WIDTH },
          padding: { xs: "1rem", sm: "1.5rem", lg: "3rem" },
        }}
      >
        <SessionProvider session={session}>
          <I18nProvider namespaces={["common"]}>
              <AutoLogoutProvider/>
              <RefreshTokenProvider/>
              <Stack spacing={2}>
                <Breadcrumb />
                {children}
              </Stack>
          </I18nProvider>
        </SessionProvider>
      </Box>
    </>
  );
}
