import { SubscriptionPage } from "./pages/SubscriptionPage";
import { LoginConfigPage } from "./pages/LoginConfigPage";
import { lazy, type ReactNode } from "react";

// 路由注册表 —— 控制台的单一事实源：导航 + 内容都从这里派生。
// 新增页面 = 在此加一条；real 页面用 lazy 懒加载（代码分割）。


const DashboardPage = lazy(() => import("./pages/DashboardPage").then((m) => ({ default: m.DashboardPage })));
const ModelConfigPage = lazy(() => import("./pages/ModelConfigPage").then((m) => ({ default: m.ModelConfigPage })));
const DatasourcesPage = lazy(() => import("./pages/DatasourcesPage").then((m) => ({ default: m.DatasourcesPage })));
const StatusPage = lazy(() => import("./pages/StatusPage").then((m) => ({ default: m.StatusPage })));
const TasksPage = lazy(() => import("./pages/TasksPage").then((m) => ({ default: m.TasksPage })));
const EvalsPage = lazy(() => import("./pages/EvalsPage").then((m) => ({ default: m.EvalsPage })));
const TenantsPage = lazy(() => import("./pages/TenantsPage").then((m) => ({ default: m.TenantsPage })));
const GuardrailsPage = lazy(() => import("./pages/GuardrailsPage").then((m) => ({ default: m.GuardrailsPage })));
const PromptsPage = lazy(() => import("./pages/PromptsPage").then((m) => ({ default: m.PromptsPage })));
const AuditPage = lazy(() => import("./pages/AuditPage").then((m) => ({ default: m.AuditPage })));
const PaymentConfigPage = lazy(() => import("./pages/PaymentConfigPage").then((m) => ({ default: m.PaymentConfigPage })));
const PaymentOrdersPage = lazy(() => import("./pages/PaymentOrdersPage").then((m) => ({ default: m.PaymentOrdersPage })));
const SpacesPage = lazy(() => import("./pages/SpacesPage").then((m) => ({ default: m.SpacesPage })));

export interface RouteDef {
  path: string;
  label: string;
  group: string;
  /** 二级菜单名。同 group 内 parent 相同的若干条会聚成一个可展开子菜单（如 运维 > 支付 > 配置/订单与对账）。 */
  parent?: string;
  ready?: boolean;
  element: ReactNode;
}

/** 侧栏一个节点：要么是单条路由，要么是带子项的可展开二级菜单。 */
export type NavNode =
  | { kind: "item"; route: RouteDef }
  | { kind: "parent"; label: string; items: RouteDef[] };

export const routes: RouteDef[] = [
  {
    path: "dashboard",
    label: "仪表盘",
    group: "分析",
    ready: true,
    element: <DashboardPage />,
  },
  {
    path: "status",
    label: "服务状态",
    group: "运维",
    ready: true,
    element: <StatusPage />,
  },
  {
    path: "tasks",
    label: "任务观测",
    group: "运维",
    ready: true,
    element: <TasksPage />,
  },
  {
    path: "evals",
    label: "自动评测",
    group: "运维",
    ready: true,
    element: <EvalsPage />,
  },
  {
    path: "audit",
    label: "审计 & 安全",
    group: "运维",
    ready: true,
    element: <AuditPage />,
  },
  {
    path: "models",
    label: "模型配置",
    group: "运维",
    ready: true,
    element: <ModelConfigPage />,
  },
  {
    path: "login-config",
    label: "登录设置",
    group: "运维",
    ready: true,
    element: <LoginConfigPage />,
  },
  {
    path: "datasources",
    label: "数据源 & RAG",
    group: "运维",
    ready: true,
    element: <DatasourcesPage />,
  },
  {
    path: "prompts",
    label: "提示词",
    group: "运维",
    ready: true,
    element: <PromptsPage />,
  },
  {
    path: "payment/config",
    label: "配置",
    group: "运维",
    parent: "支付",
    ready: true,
    element: <PaymentConfigPage />,
  },
  {
    path: "payment/subscription",
    label: "订阅",
    group: "运维",
    parent: "支付",
    ready: true,
    element: <SubscriptionPage />,
  },
  {
    path: "payment/orders",
    label: "订单与对账",
    group: "运维",
    parent: "支付",
    ready: true,
    element: <PaymentOrdersPage />,
  },
  {
    path: "tenants",
    label: "租户 & 用户",
    group: "平台",
    ready: true,
    element: <TenantsPage />,
  },
  {
    path: "spaces",
    label: "空间",
    group: "平台",
    ready: true,
    element: <SpacesPage />,
  },
  {
    path: "guardrails",
    label: "安全护栏",
    group: "平台",
    ready: true,
    element: <GuardrailsPage />,
  },
];

export const defaultPath = "dashboard";

// 派生分组导航（保持注册顺序）。
export function navGroups(): Array<{ group: string; nodes: NavNode[] }> {
  const out: Array<{ group: string; nodes: NavNode[] }> = [];
  for (const r of routes) {
    let g = out.find((x) => x.group === r.group);
    if (!g) {
      g = { group: r.group, nodes: [] };
      out.push(g);
    }
    if (!r.parent) {
      g.nodes.push({ kind: "item", route: r });
      continue;
    }
    // 同 parent 的归到同一个可展开节点（首次出现决定它在组内的位置）。
    const exist = g.nodes.find((n): n is Extract<NavNode, { kind: "parent" }> => n.kind === "parent" && n.label === r.parent);
    if (exist) exist.items.push(r);
    else g.nodes.push({ kind: "parent", label: r.parent, items: [r] });
  }
  return out;
}
