import InitColorSchemeScript from '@mui/material/InitColorSchemeScript';
import ThemeRegistry from '@/components/ThemeRegistry'; 
import Navbar from '@/components/layout/Navbar'; 
import { auth } from "@/auth"; 
import "./globals.css";

export const metadata = {
  title: "WebCalibre 2",
};

export default async function RootLayout({ children }: { children: React.ReactNode }) {
  // Fetch the session server-side to prevent UI flickering
  const session = await auth();

  /**
   * We enhance the user object before passing it to the Navbar.
   * This ensures the Navbar knows if the current user is the "Bootstrap Admin"
   * defined in our environment variables.
   */
  const navbarUser = session?.user ? {
    ...session.user,
    // Add the bootstrap flag for administrative UI access
    isBootstrap: session.user.email === process.env.INITIAL_ADMIN_EMAIL
  } : undefined;

  return (
    <html lang="en" suppressHydrationWarning>
      <body>
        <InitColorSchemeScript attribute="class" /> 
        
        {/* ThemeRegistry handles the MUI Theme and Cache Provider */}
        <ThemeRegistry>
          {/* We pass navbarUser (which includes role and isBootstrap) 
              so the Drawer knows whether to show the Settings link.
          */}
          <Navbar user={navbarUser} />
          
          <main style={{ minHeight: '100vh' }}>
            {children}
          </main>
        </ThemeRegistry>
      </body>
    </html>
  );
}