import { redirect } from "next/navigation";

import { hasAdminPermission } from "@turbostarter/auth";
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 { AdminSidebar } from "~/modules/admin/layout/sidebar";
import { DashboardInset } from "~/modules/common/layout/dashboard/inset";

const menu = [
  {
    label: "admin",
    items: [
      {
        title: "home",
        href: pathsConfig.admin.index,
        icon: Icons.Home,
      },
      {
        title: "users",
        href: pathsConfig.admin.users.index,
        icon: Icons.UsersRound,
      },
      {
        title: "organizations",
        href: pathsConfig.admin.organizations.index,
        icon: Icons.Building,
      },
      {
        title: "customers",
        href: pathsConfig.admin.customers.index,
        icon: Icons.HandCoins,
      },
    ],
  },
  {
    label: "mesh",
    items: [
      {
        title: "meshes",
        href: pathsConfig.admin.meshes.index,
        icon: Icons.Share,
      },
      {
        title: "sessions",
        href: pathsConfig.admin.sessions.index,
        icon: Icons.Activity,
      },
      {
        title: "invites",
        href: pathsConfig.admin.invites.index,
        icon: Icons.Link,
      },
      {
        title: "audit",
        href: pathsConfig.admin.audit.index,
        icon: Icons.ScrollText,
      },
    ],
  },
];

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

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

  if (!hasAdminPermission(user)) {
    return redirect(pathsConfig.dashboard.user.index);
  }

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