import type { Metadata } from 'next';
import { requireUser } from '@/lib/auth/dal';
import { isAdmin } from '@/lib/auth/roles';
import { PAYMENTS_ENABLED } from '@/lib/features';
import { getMessages } from '@/i18n/server';
import { DashboardShell, type ShellNavGroup } from '@/components/layout/DashboardShell';

export async function generateMetadata(): Promise<Metadata> {
  return { title: (await getMessages()).account.meta, robots: { index: false } };
}

export default async function AccountLayout({ children }: { children: React.ReactNode }) {
  const user = await requireUser('/akun');
  const t = (await getMessages()).dash;
  const n = t.userNav;

  const groups: ShellNavGroup[] = [
    {
      label: t.shell.groups.main,
      items: [
        { href: '/akun', label: n.overview, icon: 'home', exact: true },
        { href: '/akun/laporan', label: n.reports, icon: 'reports' },
        ...(PAYMENTS_ENABLED ? [{ href: '/akun/afiliasi', label: n.affiliate, icon: 'affiliate' as const }] : []),
      ],
    },
    {
      label: t.shell.groups.account,
      items: [
        ...(PAYMENTS_ENABLED ? [{ href: '/akun/pesanan', label: n.orders, icon: 'orders' as const }] : []),
        { href: '/akun/profil', label: n.profile, icon: 'profile' },
      ],
    },
    ...(isAdmin(user.role)
      ? [{ label: t.shell.groups.switch, items: [{ href: '/admin', label: n.admin, icon: 'admin' as const }] }]
      : []),
  ];

  return (
    <DashboardShell user={user} groups={groups}>
      {children}
    </DashboardShell>
  );
}
