import { getSession } from '@admin/auth/middleware'
import { AdminSidebar } from '@admin/components/layouts/admin-sidebar'
import { SidebarProvider } from '@admin/components/ui/sidebar'
import { admin } from '@admin/utils/app/admin'
import { canAccessAdmin } from '@admin/utils/auth/can-access-admin'
import { redirect } from 'next/navigation'
import { MinimalAccountShell } from './minimal-account-shell'

export async function AccountShell({ children }: { children: React.ReactNode }) {
  const session = await getSession({ disableCookieCache: true })

  if (!session?.user) {
    redirect('/admin/login')
  }

  if (!canAccessAdmin(session.user.role)) {
    return (
      <MinimalAccountShell name={session.user.name ?? admin.name}>{children}</MinimalAccountShell>
    )
  }

  return (
    <SidebarProvider className="min-h-svh">
      <AdminSidebar />
      <div className="relative flex min-w-0 flex-1 flex-col [--admin-shell-content-left:var(--sidebar-width)] peer-data-[state=collapsed]:[--admin-shell-content-left:var(--sidebar-width-icon)]">
        {children}
      </div>
    </SidebarProvider>
  )
}
