'use client';

import { useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { useSuperAdmin } from '@/app/_lib/super-admin-context';
import { PortalShell } from '@/app/_components/portal-shell';
import { LayoutDashboard, Building2, Network, Server, FileText } from 'lucide-react';

const NAV = [
  { href: '/admin', label: 'Dashboard', icon: <LayoutDashboard /> },
  { href: '/admin/orgs', label: 'Organisations', icon: <Network /> },
  { href: '/admin/tenants', label: 'Chapters', icon: <Building2 /> },
  { href: '/admin/bots', label: 'Bot Pool', icon: <Server /> },
  { href: '/admin/drafts', label: 'AI Drafts', icon: <FileText /> },
];

export default function AdminAuthedLayout({ children }: { children: React.ReactNode }) {
  const { admin, loading, logout } = useSuperAdmin();
  const router = useRouter();

  useEffect(() => {
    if (!loading && !admin) router.replace('/admin/login');
  }, [admin, loading, router]);

  return (
    <PortalShell
      portalName="Super Admin"
      theme="slate"
      navItems={NAV}
      loading={loading}
      authed={!!admin}
      user={admin ? { email: admin.email, name: admin.name, role: 'Super Admin' } : undefined}
      onLogout={() => { void logout(); router.replace('/admin/login'); }}
    >
      {children}
    </PortalShell>
  );
}
