import { requireRole } from '@/lib/auth/dal';
import { getMessages } from '@/i18n/server';
import { PAYMENTS_ENABLED } from '@/lib/features';
import { DashboardShell, type ShellNavGroup } from '@/components/layout/DashboardShell';

export default async function AdminPanelLayout({ children }: { children: React.ReactNode }) {
  const user = await requireRole(['ADMIN', 'SUPER_ADMIN'], '/admin');
  const t = (await getMessages()).dash;
  const n = t.adminNav;
  const isSuper = user.role === 'SUPER_ADMIN';

  const groups: ShellNavGroup[] = [
    {
      label: t.shell.groups.main,
      items: [
        { href: '/admin', label: n.dashboard, icon: 'dashboard', exact: true },
        { href: '/admin/sesi', label: n.sessions, icon: 'sessions' },
        { href: '/admin/laporan', label: n.reports, icon: 'reports' },
      ],
    },
    ...(isSuper
      ? [
          {
            label: t.shell.groups.manage,
            items: [
              { href: '/admin/pengguna', label: n.users, icon: 'users' as const },
              ...(PAYMENTS_ENABLED
                ? [
                    { href: '/admin/transaksi', label: n.orders, icon: 'orders' as const },
                    { href: '/admin/afiliasi', label: n.affiliate, icon: 'affiliate' as const },
                  ]
                : []),
            ],
          },
        ]
      : []),
    {
      label: t.shell.groups.switch,
      items: [{ href: '/akun', label: n.myAccount, icon: 'profile' }],
    },
  ];

  return (
    <DashboardShell user={user} groups={groups} area={t.roles[user.role]}>
      {children}
    </DashboardShell>
  );
}
