import AppBar from "@/components/AppBar";
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";

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, lg: NAVIGATION_CONTENT_WIDTH },
        }}
      >
        {children}
      </Box>
    </>
  );
}
