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' | 'schedule_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: 'schedule_planner',
    name: 'SCHEDULE PLANNER',
    role: '日程规划师',
    roleKey: 'schedule_planner',
    description: '负责分析当前安排，并在你明确要求时直接创建 reminder、task、todo、goal，把日程建议真正落到系统里',
    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[] = ['schedule_planner', 'executor', 'librarian', 'analyst']

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

export const RELATION_LABELS: Record<string, string> = {
  'master-schedule_planner': '安排日程',
  'master-executor': '执行指令',
  'master-librarian': '查询知识',
  'master-analyst': '请求分析',
  'schedule_planner-schedule_analysis': '分析优先级',
  'schedule_planner-schedule_planning': '编排日程',
  '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: ['schedule_planner', 'schedule_analysis', 'schedule_planning'],
    stateLabel: 'SCHEDULE',
  },
  {
    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: 'schedule_analysis',
    parentId: 'schedule_planner',
    name: 'ANALYSIS',
    role: '日程分析员',
    description: '负责分析对话历史、任务看板、Schedule Center 数据与论坛动态，识别优先级、冲突与压力点。',
    relationLabel: RELATION_LABELS['schedule_planner-schedule_analysis'],
    toolScopeLabel: 'Schedule Read',
  },
  {
    id: 'schedule_planning',
    parentId: 'schedule_planner',
    name: 'PLANNING',
    role: '日程编排员',
    description: '负责把分析结果转成今天与近期可执行安排；当你明确要求时，直接创建提醒/任务/待办/目标并返回结果。',
    relationLabel: RELATION_LABELS['schedule_planner-schedule_planning'],
    toolScopeLabel: 'Schedule Write',
  },
  {
    id: 'executor_tasks',
    parentId: 'executor',
    name: 'TASK OPS',
    role: '任务执行官',
    description: '负责调用任务、提醒、待办、目标相关工具，推进创建、更新与状态执行。',
    relationLabel: RELATION_LABELS['executor-executor_tasks'],
    toolScopeLabel: 'Task / Schedule',
  },
  {
    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[]> = {
  schedule_planner: SUB_COMMANDERS.filter((item) => item.parentId === 'schedule_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'),
}
