/**
 * AI 工具授权管理 - 可用工具全量清单（v2.3）
 *
 * 与 OpenClaw src/agents/tool-catalog.ts 保持同步。
 * 变更时需双边更新。
 */

export type ToolCategory =
  | 'core'
  | 'fs'
  | 'runtime'
  | 'sessions'
  | 'memory'
  | 'web'
  | 'media'
  | 'automation'
  | 'browser'
  | 'productivity';

export interface AvailableTool {
  name: string;
  label: string;
  description: string;
  category: ToolCategory;
  locked: boolean;
}

/**
 * LOCKED_SET: 不可编辑的最小对话工具集。
 * 前端 disabled，后端保存时强制合入。
 * 取消任一会破坏 Teams DM 正常对话。
 */
export const LOCKED_TOOL_NAMES = [
  'session_status',
  'sessions_history',
  'sessions_list',
  'sessions_send',
] as const;

export const LOCKED_SET = new Set<string>(LOCKED_TOOL_NAMES);

export const STATIC_AVAILABLE_TOOLS: AvailableTool[] = [
  // ── core (locked) ──
  { name: 'session_status', label: '会话状态', description: 'Agent 运行时自检', category: 'core', locked: true },
  { name: 'sessions_history', label: '会话历史', description: '读取多轮对话上下文', category: 'core', locked: true },
  { name: 'sessions_list', label: '会话列表', description: '列出当前会话', category: 'core', locked: true },
  { name: 'sessions_send', label: '发送回复', description: '向用户发送消息', category: 'core', locked: true },

  // ── fs ──
  { name: 'read', label: '读取文件', description: '读取文件内容', category: 'fs', locked: false },
  { name: 'write', label: '写入文件', description: '创建或覆盖文件', category: 'fs', locked: false },
  { name: 'edit', label: '编辑文件', description: '精确编辑文件', category: 'fs', locked: false },
  { name: 'apply_patch', label: '应用补丁', description: '补丁文件（OpenAI 兼容）', category: 'fs', locked: false },

  // ── runtime ──
  { name: 'exec', label: '执行命令', description: '运行 shell 命令', category: 'runtime', locked: false },
  { name: 'process', label: '进程管理', description: '管理后台进程', category: 'runtime', locked: false },

  // ── sessions (non-locked) ──
  { name: 'sessions_spawn', label: '创建子代理', description: '生成 sub-agent', category: 'sessions', locked: false },
  { name: 'sessions_yield', label: '等待子代理', description: '接收 sub-agent 结果', category: 'sessions', locked: false },
  { name: 'subagents', label: '子代理管理', description: '管理 sub-agents', category: 'sessions', locked: false },

  // ── memory ──
  { name: 'memory_search', label: '记忆搜索', description: '语义搜索记忆', category: 'memory', locked: false },
  { name: 'memory_get', label: '读取记忆', description: '读取记忆文件', category: 'memory', locked: false },

  // ── web ──
  { name: 'web_search', label: '网页搜索', description: '互联网搜索', category: 'web', locked: false },
  { name: 'web_fetch', label: '网页抓取', description: '抓取指定 URL 内容', category: 'web', locked: false },

  // ── media ──
  { name: 'image', label: '图像理解', description: '图像理解与分析', category: 'media', locked: false },
  { name: 'tts', label: '语音合成', description: '文字转语音', category: 'media', locked: false },

  // ── automation ──
  { name: 'cron', label: '定时任务', description: '创建和管理定时任务', category: 'automation', locked: false },

  // ── browser ──
  { name: 'browser', label: '浏览器', description: '沙箱浏览器，可访问企业内部 Web 应用', category: 'browser', locked: false },

  // ── productivity (M365) ──
  { name: 'm365_mail', label: '邮件（M365）', description: 'Microsoft 365 Outlook 邮件只读访问', category: 'productivity', locked: false },
  { name: 'm365_calendar', label: '日历（M365）', description: 'Microsoft 365 Outlook 日历只读访问', category: 'productivity', locked: false },
  { name: 'm365_files', label: '文件（M365/SharePoint）', description: 'Microsoft 365 SharePoint/OneDrive 文件只读访问', category: 'productivity', locked: false },
];

export const AVAILABLE_TOOL_NAMES = new Set(STATIC_AVAILABLE_TOOLS.map((t) => t.name));

/**
 * 新建角色 / backfill 时使用的默认基线工具集（v2.3 = 全量可用工具）。
 * 含 LOCKED_SET 4 个核心对话工具 + 其余 20 个非锁定工具。
 * 单一来源：派生自 STATIC_AVAILABLE_TOOLS，禁止在别处硬编码副本。
 */
export const EMPLOYEE_BASELINE_TOOLS: readonly string[] = STATIC_AVAILABLE_TOOLS.map(
  (t) => t.name,
);

export const CATEGORY_LABELS: Record<ToolCategory, string> = {
  core: '核心对话工具',
  fs: '文件',
  runtime: '运行时',
  sessions: '会话管理',
  memory: '记忆',
  web: '网页',
  media: '媒体',
  automation: '自动化',
  browser: '浏览器',
  productivity: 'M365',
};
