import type { ReactNode } from 'react';
import { AppHeader } from '@/components/host/AppHeader';
import type { NavItem } from '@/components/host/client';
import { getAdminContext } from '@/lib/auth/session';
import { signOutAdmin } from '../login/actions';

const NAV: readonly NavItem[] = [
  { href: '/admin', label: 'Overview', exact: true },
  { href: '/admin/venues', label: 'Venues' },
  { href: '/admin/packs', label: 'Packs' },
  { href: '/admin/plans', label: 'Plans' },
  { href: '/admin/leads', label: 'Pilot leads' },
  { href: '/admin/support', label: 'Support' },
];

/** Platform admin chrome. Pages, actions and route handlers still call requireAdmin() themselves. */
export default async function AdminAppLayout({ children }: { children: ReactNode }) {
  // Not signed in: the page's own requireAdmin(next) redirects (keeping the destination).
  const ctx = await getAdminContext();
  if (!ctx) return children;
  const { admin } = ctx;
  return (
    <div className="flex min-h-dvh flex-col">
      <AppHeader
        homeHref="/admin"
        context="Platform admin"
        contextLabel="PintRush staff"
        nav={NAV}
        navLabel="Platform admin"
        userLabel={admin.email}
        signOutAction={signOutAdmin}
      />
      <main id="main" tabIndex={-1} className="container-page flex-1 py-8 outline-none sm:py-10">
        {children}
      </main>
    </div>
  );
}
