import { redirect } from "next/navigation";
import { cookies } from "next/headers";
import { getCurrentRole } from "@/lib/auth";
import BackendShell from "@/components/backend-shell";

// Platform-staff surface. Gated by role: only role = 'admin' may enter. A
// logged-in organizer who guesses an /admin URL is redirected to their
// dashboard.
export default async function AdminLayout({ children }: { children: React.ReactNode }) {
  const { user, isAdmin } = await getCurrentRole();

  if (!user) {
    redirect("/login");
  }
  if (!isAdmin) {
    redirect("/dashboard?notice=admin-required");
  }

  const theme = (await cookies()).get("be-theme")?.value === "light" ? "light" : "dark";

  return (
    <BackendShell role="admin" userEmail={user.email ?? ""} initialTheme={theme}>
      {children}
    </BackendShell>
  );
}
