import type React from 'react'
import { DashboardOutlined, RobotOutlined, UserOutlined, DollarOutlined, BookOutlined, ImportOutlined, FileOutlined, SafetyOutlined, CodeOutlined, CloudServerOutlined, SettingOutlined } from '@ant-design/icons'
import type { AdminRole } from '@/types/admin'
import { hasRole } from '@/constants/roles'

export interface AdminMenuItem {
  path: string
  name: string
  icon?: React.ReactNode
  requiredRole?: AdminRole
  children?: AdminMenuItem[]
}

export const adminMenuItems: AdminMenuItem[] = [
  { path: '/', name: '总览', icon: <DashboardOutlined /> },
  { path: '/assistant', name: '任务助理', icon: <RobotOutlined /> },
  { path: '/hermes', name: 'Hermes 设置', icon: <RobotOutlined />, requiredRole: 'SUPER_ADMIN' },
  { path: '/users', name: '用户管理', icon: <UserOutlined />, children: [
    { path: '/users/admins', name: '管理员', requiredRole: 'SUPER_ADMIN' },
    { path: '/users/members', name: '普通用户' },
  ]},
  { path: '/membership', name: '会员与额度', icon: <DollarOutlined />, requiredRole: 'ADMIN' },
  { path: '/knowledge', name: '知识库管理', icon: <BookOutlined />, children: [
    { path: '/knowledge/bases', name: '知识库列表' },
    { path: '/knowledge/sources', name: '知识源列表' },
    { path: '/knowledge/ops', name: '知识运维' },
  ]},
  { path: '/imports', name: '文档导入', icon: <ImportOutlined /> },
  { path: '/files', name: '文件与 COS', icon: <FileOutlined /> },
  { path: '/audit', name: '审计日志', icon: <SafetyOutlined />, requiredRole: 'ADMIN' },
  { path: '/billing', name: 'API 用量', icon: <DollarOutlined />, requiredRole: 'SUPER_ADMIN' },
  { path: '/git', name: '代码仓库', icon: <CodeOutlined /> },
  { path: '/ops', name: '系统运维', icon: <CloudServerOutlined />, requiredRole: 'SUPER_ADMIN', children: [
    { path: '/throttle', name: '限流管理' },
    { path: '/security-events', name: '安全事件' },
    { path: '/secrets', name: '密钥管理' },
    { path: '/metrics', name: '接口监控' },
    { path: '/ai-gateway', name: 'AI Gateway' },
    { path: '/servers', name: '服务器' },
    { path: '/chat-logs', name: '对话日志' },
    { path: '/events', name: '事件队列' },
    { path: '/vector', name: '向量检索' },
    { path: '/config', name: '配置管理' },
    { path: '/safety', name: '内容安全' },
  ]},
  { path: '/settings', name: '系统配置', icon: <SettingOutlined />, requiredRole: 'ADMIN' },
]

export function filterMenuByRole(items: AdminMenuItem[], role?: AdminRole): AdminMenuItem[] {
  return items.filter(item => {
    if (item.requiredRole && role && !hasRole(role, item.requiredRole)) return false
    if (item.children) item.children = filterMenuByRole(item.children, role)
    return true
  })
}
