import { prisma } from '@/lib/prisma'
import { requireRole } from '@/lib/auth-redirect'
import { AdminSidebar } from '@/components/layouts/admin-sidebar'
import { AdminEditionWrapper } from '@/components/layouts/admin-edition-wrapper'

export default async function AdminLayout({
  children,
}: {
  children: React.ReactNode
}) {
  const session = await requireRole('SUPER_ADMIN', 'PROGRAM_ADMIN')

  // Fetch all editions (programs) for the edition selector
  const editions = await prisma.program.findMany({
    select: {
      id: true,
      name: true,
      year: true,
      status: true,
    },
    orderBy: { year: 'desc' },
  })

  return (
    <AdminEditionWrapper editions={editions}>
      <div className="min-h-screen bg-background">
        <AdminSidebar
          user={{
            name: session.user.name,
            email: session.user.email,
            role: session.user.role,
          }}
        />
        <main className="lg:pl-64">
          {/* Spacer for mobile header */}
          <div className="h-16 lg:hidden" />
          <div className="container-app py-6 lg:py-8">{children}</div>
        </main>
      </div>
    </AdminEditionWrapper>
  )
}
