import { lazy } from 'react'
import { ModuleDefinition } from './types'
import { 
  MessageOutlined, 
  FileSearchOutlined,
  FolderOpenOutlined, 
  ClearOutlined,
  BarChartOutlined,
  LineChartOutlined
} from '@ant-design/icons'

/**
 * 模块注册中心
 * 按照平台架构文档顺序注册所有业务模块
 * 参考：docs/01-平台基础层/06-前端架构/01-前端总体架构设计.md
 */
export const MODULES: ModuleDefinition[] = [
  {
    id: 'ai-qa',
    name: 'AI问答',
    path: '/ai-qa',
    icon: MessageOutlined,
    component: lazy(() => import('@/modules/aia')),
    placeholder: true, // 后续重写
    requiredVersion: 'basic',
    description: '基于LLM的智能问答系统',
  },
  {
    id: 'literature-platform',
    name: 'AI智能文献',
    path: '/literature',
    icon: FileSearchOutlined,
    component: lazy(() => import('@/modules/asl')),
    placeholder: false, // Week 3 开发
    requiredVersion: 'advanced',
    description: 'AI驱动的文献筛选和分析系统',
    standalone: true, // 支持独立运行
  },
  {
    id: 'knowledge-base',
    name: '知识库',
    path: '/knowledge-base',
    icon: FolderOpenOutlined,
    component: lazy(() => import('@/modules/pkb')),
    placeholder: true, // 后续重写
    requiredVersion: 'basic',
    description: '个人知识库管理系统',
  },
  {
    id: 'data-cleaning',
    name: '智能数据清洗',
    path: '/data-cleaning',
    icon: ClearOutlined,
    component: lazy(() => import('@/modules/dc')),
    placeholder: true, // 占位
    requiredVersion: 'advanced',
    description: '智能数据清洗整理工具',
  },
  {
    id: 'statistical-analysis',
    name: '智能统计分析',
    path: '/intelligent-analysis',
    icon: BarChartOutlined,
    component: lazy(() => import('@/modules/ssa')),
    placeholder: true, // Java团队开发，前端集成
    requiredVersion: 'premium',
    description: '智能统计分析系统（Java团队开发）',
    isExternal: true, // 外部模块
  },
  {
    id: 'statistical-tools',
    name: '统计分析工具',
    path: '/statistical-tools',
    icon: LineChartOutlined,
    component: lazy(() => import('@/modules/st')),
    placeholder: true, // Java团队开发，前端集成
    requiredVersion: 'premium',
    description: '统计分析工具集（Java团队开发）',
    isExternal: true, // 外部模块
  },
]

/**
 * 根据ID获取模块
 */
export const getModuleById = (id: string): ModuleDefinition | undefined => {
  return MODULES.find(module => module.id === id)
}

/**
 * 根据路径获取模块
 */
export const getModuleByPath = (path: string): ModuleDefinition | undefined => {
  return MODULES.find(module => path.startsWith(module.path))
}

/**
 * 获取所有可用模块（根据权限过滤）
 * 
 * @param userVersion 用户版本（权限等级）
 * @returns 用户有权访问的模块列表
 * 
 * @version Week 2 Day 7 - 任务17：实现权限过滤逻辑
 */
export const getAvailableModules = (userVersion: string = 'premium'): ModuleDefinition[] => {
  // 权限等级映射
  const versionLevel: Record<string, number> = {
    basic: 1,
    advanced: 2,
    premium: 3,
  }
  
  const currentLevel = versionLevel[userVersion] || 0
  
  // 过滤出用户有权限访问的模块
  return MODULES.filter(module => {
    // 如果模块没有权限要求，所有人都可以访问
    if (!module.requiredVersion) return true
    
    // 检查用户权限等级是否满足模块要求
    const requiredLevel = versionLevel[module.requiredVersion] || 0
    return currentLevel >= requiredLevel
  })
}

