import { requireAdmin } from "@/lib/session"
import { AdminSidebar } from "@/components/admin/admin-sidebar"
import { AdminHeader } from "@/components/admin/admin-header"
import { Breadcrumbs } from "@/components/dashboard/breadcrumbs"
import { PageTransition } from "@/components/dashboard/page-transition"
import { ScrollToTop } from "@/components/ui/scroll-to-top"

export const dynamic = "force-dynamic"

export default async function AdminLayout({ children }: { children: React.ReactNode }) {
  // Gate #2 (after proxy.ts edge check): redirects non-admins. Every
  // /api/admin route handler re-checks via getAdminSession() — gate #3.
  const { user, profile } = await requireAdmin()

  return (
    <div className="flex h-screen overflow-hidden bg-[#09090b]">
      <AdminSidebar email={profile?.email ?? user.email} name={profile?.full_name ?? user.name ?? ""} />
      <div className="flex flex-1 flex-col overflow-hidden">
        <AdminHeader />
        <main id="main-scroll" className="flex-1 overflow-y-auto p-4 sm:p-6">
          <Breadcrumbs />
          <PageTransition>{children}</PageTransition>
        </main>
      </div>
      <ScrollToTop />
    </div>
  )
}
