import { DashShell, NavItem } from "@/components/dash/Shell";
import { requireUser } from "@/lib/auth";
import { get } from "@/lib/db";

export const dynamic = "force-dynamic";
export const metadata = { title: "My portal" };
export default async function PortalLayout({ children }: { children: React.ReactNode }) {
  const u = await requireUser(["customer"]);
  const unread = get<any>("SELECT COUNT(*) c FROM chat_messages m JOIN chat_threads t ON t.id=m.thread_id WHERE t.user_id=? AND m.sender_role<>'customer' AND m.read_at IS NULL", u.id).c;
  const nav: { group?: string; items: NavItem[] }[] = [
    { items: [{ href: "/portal", label: "Dashboard", icon: "dash" }, { href: "/portal/products", label: "My products", icon: "products" }, { href: "/portal/orders", label: "Orders & invoices", icon: "orders" }, { href: "/portal/payments", label: "Payments", icon: "payments" }] },
    { group: "Help", items: [{ href: "/portal/support", label: "Support chat", icon: "support", badge: unread }, { href: "/portal/settings", label: "Profile & security", icon: "security" }] },
    { group: "Explore", items: [{ href: "/products", label: "Browse catalogue", icon: "store" }] },
  ];
  return <DashShell area="portal" nav={nav} user={{ name: u.name, email: u.email, role: "Customer" }}>{children}</DashShell>;
}
