export interface Agent {
  id: string
  name: string
  role: string
  roleKey: string
  description: string
  systemPrompt: string
  enabled: boolean
  isMaster?: boolean
}

export interface SubCommander {
  id: string
  parentId: MainAgentId
  name: string
  role: string
  description: string
  relationLabel: string
  toolScopeLabel: string
}

export interface CommanderSkill {
  id: string
  label: string
  title: string
  description: string
  relatedAgentIds: string[]
  stateLabel: string
}

export type MainAgentId = 'master' | 'planner' | 'executor' | 'librarian' | 'analyst'

export const DEFAULT_AGENTS: Agent[] = [
  {
    id: 'master',
    name: 'JARVIS',
    role: '战略中枢',
    roleKey: 'master',
    description: '负责理解目标、判断路径并协调各子 Agent 推进，不是普通助手，而是整体协作的中枢',
    systemPrompt: '你是 Jarvis 的战略中枢。你的职责不是机械分发任务，而是先看清用户真正要解决的问题，再协调规划、执行、知识与分析能力，把复杂度压平，并给出清晰、可推进的回应。',
    enabled: true,
    isMaster: true,
  },
  {
    id: 'planner',
    name: 'PLANNER',
    role: '路径规划师',
    roleKey: 'planner',
    description: '负责拆解复杂目标、安排顺序、收束执行路径，让事情变得清楚可做',
    systemPrompt: '你是 Jarvis 的路径规划师。面对复杂目标时，先识别约束和优先级，再把任务拆成清晰、可执行的步骤，帮助用户迅速看清最短可行路径。',
    enabled: true,
  },
  {
    id: 'executor',
    name: 'EXECUTOR',
    role: '执行推进者',
    roleKey: 'executor',
    description: '负责调用工具落实具体操作，关注结果、边界和下一步，而不是只回报动作',
    systemPrompt: '你是 Jarvis 的执行推进者。根据当前目标调用工具完成具体操作，明确说明已经做了什么、结果如何、还差什么，并把下一步交代清楚。',
    enabled: true,
  },
  {
    id: 'librarian',
    name: 'LIBRARIAN',
    role: '知识统筹者',
    roleKey: 'librarian',
    description: '负责检索、连接和梳理知识，让信息不只是被找到，而是被理解和利用',
    systemPrompt: '你是 Jarvis 的知识统筹者。你的职责是检索用户的知识库与相关上下文，提炼重点、连接线索，并在证据范围内给出可靠回答。',
    enabled: true,
  },
  {
    id: 'analyst',
    name: 'ANALYST',
    role: '洞察分析师',
    roleKey: 'analyst',
    description: '负责从数据和状态里提炼趋势、风险与判断，支持更高质量的决策',
    systemPrompt: '你是 Jarvis 的洞察分析师。当用户需要分析、统计、总结或判断趋势时，你要从数据里提炼真正有用的结论，并给出可执行的判断与建议。',
    enabled: true,
  },
]

export const MAIN_AGENT_ORDER: MainAgentId[] = ['planner', 'executor', 'librarian', 'analyst']

export const AGENT_RELATIONS: Record<MainAgentId, string[]> = {
  master: ['planner', 'executor', 'librarian', 'analyst'],
  planner: ['planner_scope', 'planner_steps'],
  executor: ['executor_tasks', 'executor_forum'],
  librarian: ['librarian_retrieval', 'librarian_graph'],
  analyst: ['analyst_progress', 'analyst_insights'],
}

export const RELATION_LABELS: Record<string, string> = {
  'master-planner': '调度任务',
  'master-executor': '执行指令',
  'master-librarian': '查询知识',
  'master-analyst': '请求分析',
  'planner-planner_scope': '收束目标',
  'planner-planner_steps': '拆解路径',
  'executor-executor_tasks': '调度任务工具',
  'executor-executor_forum': '调度论坛工具',
  'librarian-librarian_retrieval': '检索知识',
  'librarian-librarian_graph': '沉淀图谱',
  'analyst-analyst_progress': '汇总进度',
  'analyst-analyst_insights': '生成洞察',
}

export const COMMANDER_SKILLS: CommanderSkill[] = [
  {
    id: 'skill_orchestration',
    label: 'CORE-01',
    title: '战略编排',
    description: '统一调度主控链路，决定由谁接管当前任务。',
    relatedAgentIds: ['master'],
    stateLabel: 'ROUTING',
  },
  {
    id: 'skill_planner',
    label: 'CORE-02',
    title: '路径拆解',
    description: '把复杂目标压缩成清晰路径、优先级与执行步骤。',
    relatedAgentIds: ['planner', 'planner_scope', 'planner_steps'],
    stateLabel: 'STRUCTURE',
  },
  {
    id: 'skill_executor',
    label: 'CORE-03',
    title: '执行调度',
    description: '驱动任务工具与交互动作，把计划转成实际推进。',
    relatedAgentIds: ['executor', 'executor_tasks', 'executor_forum'],
    stateLabel: 'ACTION',
  },
  {
    id: 'skill_librarian',
    label: 'CORE-04',
    title: '知识检索',
    description: '连接知识与上下文，让信息可以被实时调取和利用。',
    relatedAgentIds: ['librarian', 'librarian_retrieval', 'librarian_graph'],
    stateLabel: 'RETRIEVE',
  },
  {
    id: 'skill_analyst',
    label: 'CORE-05',
    title: '洞察研判',
    description: '从状态和数据中提炼风险、趋势与行动建议。',
    relatedAgentIds: ['analyst', 'analyst_progress', 'analyst_insights'],
    stateLabel: 'INSIGHT',
  },
]

export const SUB_COMMANDERS: SubCommander[] = [
  {
    id: 'planner_scope',
    parentId: 'planner',
    name: 'SCOPE',
    role: '目标收束官',
    description: '负责澄清目标、识别边界与约束，让规划前提先变清楚。',
    relationLabel: RELATION_LABELS['planner-planner_scope'],
    toolScopeLabel: '约束/需求',
  },
  {
    id: 'planner_steps',
    parentId: 'planner',
    name: 'STEPS',
    role: '步骤拆解官',
    description: '负责把目标拆成步骤、优先级与依赖关系，形成执行路径。',
    relationLabel: RELATION_LABELS['planner-planner_steps'],
    toolScopeLabel: '路径/排序',
  },
  {
    id: 'executor_tasks',
    parentId: 'executor',
    name: 'TASK OPS',
    role: '任务执行官',
    description: '负责调用任务相关工具，推进创建、更新与状态执行。',
    relationLabel: RELATION_LABELS['executor-executor_tasks'],
    toolScopeLabel: 'Task Tools',
  },
  {
    id: 'executor_forum',
    parentId: 'executor',
    name: 'FORUM OPS',
    role: '论坛执行官',
    description: '负责处理论坛发帖、扫描指令等交互型执行操作。',
    relationLabel: RELATION_LABELS['executor-executor_forum'],
    toolScopeLabel: 'Forum Tools',
  },
  {
    id: 'librarian_retrieval',
    parentId: 'librarian',
    name: 'RETRIEVAL',
    role: '检索问答官',
    description: '负责检索知识库与上下文，快速给出证据范围内的回答。',
    relationLabel: RELATION_LABELS['librarian-librarian_retrieval'],
    toolScopeLabel: 'Search / Hybrid',
  },
  {
    id: 'librarian_graph',
    parentId: 'librarian',
    name: 'GRAPH LINK',
    role: '图谱沉淀官',
    description: '负责知识图谱构建、关系串联与结构化沉淀。',
    relationLabel: RELATION_LABELS['librarian-librarian_graph'],
    toolScopeLabel: 'Graph Context',
  },
  {
    id: 'analyst_progress',
    parentId: 'analyst',
    name: 'PROGRESS',
    role: '进度研判官',
    description: '负责汇总任务与讨论状态，判断阶段进展与卡点。',
    relationLabel: RELATION_LABELS['analyst-analyst_progress'],
    toolScopeLabel: 'Status / Tasks',
  },
  {
    id: 'analyst_insights',
    parentId: 'analyst',
    name: 'INSIGHTS',
    role: '洞察建议官',
    description: '负责提炼趋势、风险与建议，把数据转成判断。',
    relationLabel: RELATION_LABELS['analyst-analyst_insights'],
    toolScopeLabel: 'Trends / Advice',
  },
]

export const SUB_COMMANDERS_BY_PARENT: Record<Exclude<MainAgentId, 'master'>, SubCommander[]> = {
  planner: SUB_COMMANDERS.filter((item) => item.parentId === 'planner'),
  executor: SUB_COMMANDERS.filter((item) => item.parentId === 'executor'),
  librarian: SUB_COMMANDERS.filter((item) => item.parentId === 'librarian'),
  analyst: SUB_COMMANDERS.filter((item) => item.parentId === 'analyst'),
}
