/**
 * Navigation Configuration
 * 导航配置 - 集中管理导航菜单项
 */

import { Home, Settings, Server, Database, LineChart, Users, Rocket, Boxes } from "lucide-react";
import type { ReactNode } from "react";
import type { User } from "../providers/AuthContext";
import { canAccessRoute } from "../providers/auth-model";

/**
 * Navigation item type
 */
export interface NavItem {
  key: string;
  label: string;
  icon: ReactNode;
  active?: boolean;
  onClick?: () => void;
  children?: NavItem[];
}

/**
 * Page info type for header display
 */
export interface PageInfo {
  title: string;
  icon: ReactNode;
}

/**
 * Get navigation items
 * @param currentPath - Current route path
 * @param navigate - Navigation function
 * @returns Navigation items array
 */
export const getNavItems = (
  currentPath: string,
  navigate: (path: string) => void,
  user?: User | null
): NavItem[] => {
  const canAccess = (path: string) => (user ? canAccessRoute(path, user) : true);
  const items: NavItem[] = [
    {
      key: "home",
      label: "Home",
      icon: <Home className="w-4 h-4 text-secondary" />,
      active: currentPath === "/home",
      onClick: () => navigate("/home"),
    },
    {
      key: "artifact-registries",
      label: "Launch Instance",
      icon: <Rocket className="w-4 h-4 text-blue-600" />,
      active: currentPath === "/artifact/registries",
      onClick: () => navigate("/artifact/registries"),
    },
    {
      key: "artifact-instances",
      label: "Instances",
      icon: <Boxes className="w-4 h-4 text-emerald-600" />,
      active: currentPath === "/artifact/instances",
      onClick: () => navigate("/artifact/instances"),
    },
    {
      key: "monitoring-clusters",
      label: "Cluster Monitoring",
      icon: <LineChart className="w-4 h-4 text-accent-teal" />,
      active: currentPath === "/monitoring/clusters",
      onClick: () => navigate("/monitoring/clusters"),
    },
    {
      key: "setup",
      label: "Setup",
      icon: <Settings className="w-4 h-4 text-brand-accent" />,
      children: [
        {
          key: "configuration-clusters",
          label: "Clusters",
          icon: <Server className="w-4 h-4 text-accent-teal" />,
          active: currentPath === "/configuration/clusters",
          onClick: () => navigate("/configuration/clusters"),
        },
        {
          key: "configuration-registries",
          label: "Registries",
          icon: <Database className="w-4 h-4 text-brand-light" />,
          active: currentPath === "/configuration/registries",
          onClick: () => navigate("/configuration/registries"),
        },
        {
          key: "configuration-users",
          label: "Users",
          icon: <Users className="w-4 h-4 text-blue-600" />,
          active: currentPath === "/configuration/users",
          onClick: () => navigate("/configuration/users"),
        },
      ],
    },
  ];

  return items
    .map((item) => ({
      ...item,
      children: item.children?.filter((child) => {
        const routePath = routePathByKey[child.key];
        return routePath ? canAccess(routePath) : true;
      }),
    }))
    .filter((item) => {
      const routePath = routePathByKey[item.key];
      if (routePath && !canAccess(routePath)) {
        return false;
      }
      return !item.children || item.children.length > 0;
    });
};

const routePathByKey: Record<string, string> = {
  home: "/home",
  "artifact-registries": "/artifact/registries",
  "artifact-instances": "/artifact/instances",
  "monitoring-clusters": "/monitoring/clusters",
  "configuration-clusters": "/configuration/clusters",
  "configuration-registries": "/configuration/registries",
  "configuration-users": "/configuration/users",
};

/**
 * Get page header info based on current path
 * @param pathname - Current route pathname
 * @returns Page info object
 */
export const getPageInfo = (pathname: string): PageInfo => {
  if (pathname === "/artifact/registries") {
    return { title: "Launch Instance", icon: <Rocket className="w-6 h-6 text-blue-600" /> };
  }
  if (pathname === "/artifact/instances") {
    return { title: "Instances", icon: <Boxes className="w-6 h-6 text-emerald-600" /> };
  }
  if (pathname === "/configuration/clusters") {
    return { title: "Setup - Clusters", icon: <Server className="w-6 h-6 text-accent-teal" /> };
  }
  if (pathname === "/configuration/registries") {
    return { title: "Setup - Registries", icon: <Database className="w-6 h-6 text-brand-light" /> };
  }
  if (pathname === "/configuration/users") {
    return { title: "Setup - Users", icon: <Users className="w-6 h-6 text-blue-600" /> };
  }
  if (pathname === "/monitoring/clusters") {
    return { title: "Monitoring - Clusters", icon: <LineChart className="w-6 h-6 text-accent-teal" /> };
  }
  return { title: "OCDP Platform", icon: <Home className="w-6 h-6 text-secondary" /> };
};
