import { redirect } from "next/navigation";

import { Icons } from "@turbostarter/ui-web/icons";
import { SidebarProvider } from "@turbostarter/ui-web/sidebar";

import { pathsConfig } from "~/config/paths";
import { getSession } from "~/lib/auth/server";
import { DashboardInset } from "~/modules/common/layout/dashboard/inset";
import { DashboardSidebar } from "~/modules/common/layout/dashboard/sidebar";

/**
 * Dashboard sidebar menu configuration.
 */
const menu = [
  {
    label: "platform",
    items: [
      {
        title: "dashboard",
        href: pathsConfig.dashboard.user.index,
        icon: Icons.Atom,
      },
      {
        title: "meshes",
        href: pathsConfig.dashboard.user.meshes.index,
        icon: Icons.Share,
      },
      {
        title: "topics",
        href: pathsConfig.dashboard.user.topics,
        icon: Icons.MessageSquare,
      },
      {
        title: "notifications",
        href: pathsConfig.dashboard.user.notifications,
        icon: Icons.Bell,
      },
      {
        title: "activity",
        href: pathsConfig.dashboard.user.activity,
        icon: Icons.Activity,
      },
      {
        title: "search",
        href: pathsConfig.dashboard.user.search,
        icon: Icons.Search,
      },
      {
        title: "invites",
        href: pathsConfig.dashboard.user.invites,
        icon: Icons.Link,
      },
    ],
  },
  {
    label: "manage",
    items: [
      {
        title: "account",
        href: pathsConfig.dashboard.user.settings.index,
        icon: Icons.Settings,
      },
    ],
  },
  {
    label: "dev",
    items: [
      {
        title: "demos",
        href: pathsConfig.demo.index,
        icon: Icons.LayoutDashboard,
      },
    ],
  },
];

export default async function DashboardLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const { user } = await getSession();

  if (!user) {
    return redirect(pathsConfig.auth.login);
  }

  return (
    <SidebarProvider>
      <DashboardSidebar user={user} menu={menu} />
      <DashboardInset>{children}</DashboardInset>
    </SidebarProvider>
  );
}
