import { SidebarInset, SidebarProvider } from "@/components/ui/sidebar";
import AppHeader from "@/components/admin/common/AppHeader";
import AppSidebar from "@/components/admin/common/AppSidebar";
import { ThemeProvider } from "@/components/common/theme-provider";
import { cn } from "@/lib/utils";
import { requireAdmin } from "@/lib/auth/admin";

export const dynamic = "force-dynamic";
export const revalidate = 0;

async function DashboardLayout({ children }: { children: React.ReactNode }) {
  // Authoritative server-side admin session validation.
  // Proxy does a cheap cookie check; we do the real DB lookup here.
  await requireAdmin();

  return (
    <ThemeProvider
      attribute="class"
      defaultTheme="system"
      enableSystem
      disableTransitionOnChange
      storageKey="si-tech-hub-admin-theme"
    >
      <SidebarProvider
        open={false}
        className={cn("[--app-wrapper-max-width:80rem]")}
      >
        <AppSidebar />
        <SidebarInset>
          <AppHeader />
          <div
            className={cn(
              "flex flex-1 flex-col p-4 md:p-6",
              "mx-auto w-full max-w-(--app-wrapper-max-width)",
            )}
          >
            {children}
          </div>
        </SidebarInset>
      </SidebarProvider>
    </ThemeProvider>
  );
}

export default DashboardLayout;
