import { redirect } from "next/navigation";

import { auth } from "@/auth";
import { SideNavProvider } from "@/components/nav/side-nav-provider";
import { SideNav } from "@/components/nav/side-nav";

export default async function AppLayout({ children }: { children: React.ReactNode }) {
  const session = await auth();
  // Defense in depth: proxy.ts already redirects unauthenticated requests,
  // but every protected data boundary should check for itself too.
  if (!session?.user) redirect("/login");

  return (
    <SideNavProvider>
      <div className="flex h-screen overflow-hidden">
        <SideNav userEmail={session.user.email ?? ""} />
        <main className="flex-1 overflow-auto">{children}</main>
      </div>
    </SideNavProvider>
  );
}
