export function mergeWorkbenchAiThinkingEvents(...groups) {
  const merged = []
  const indexById = new Map()
  groups.flat().filter(Boolean).forEach((event) => {
    const eventId = String(event.eventId || event.event_id || '').trim()
    if (!eventId || !indexById.has(eventId)) {
      if (eventId) {
        indexById.set(eventId, merged.length)
      }
      merged.push(event)
      return
    }
    const index = indexById.get(eventId)
    merged.splice(index, 1, {
      ...merged[index],
      ...event,
      eventId
    })
  })
  return merged
}

export function completeWorkbenchAiThinkingEvents(events = []) {
  return events.map((event) => ({
    ...event,
    status: event.status === 'failed' ? 'failed' : 'completed'
  }))
}

export function buildInitialModelPlanningThinkingEvents() {
  return [
    {
      eventId: 'model-planning-intent',
      title: '判断办理意图',
      content: '正在判断这句话是要办申请、做报销、处理附件，还是普通咨询。',
      status: 'running'
    }
  ]
}

export function buildModelPlanningProgressSchedule() {
  return [
    {
      delayMs: 900,
      event: {
        eventId: 'model-planning-slots',
        title: '抽取关键信息',
        content: '正在整理日期、地点、事由、交通方式、附件和“保存/提交”等动作线索。',
        status: 'running'
      }
    },
    {
      delayMs: 2200,
      event: {
        eventId: 'model-planning-steps',
        title: '规划执行步骤',
        content: '正在生成可执行动作序列，例如填充申请表、校验必填项、保存草稿或发起提交。',
        status: 'running'
      }
    },
    {
      delayMs: 5200,
      event: {
        eventId: 'model-planning-tools',
        title: '匹配业务工具',
        content: '正在把模型计划映射到白名单工具，避免未确认的提交或附件关联直接产生副作用。',
        status: 'running'
      }
    },
    {
      delayMs: 9000,
      event: {
        eventId: 'model-planning-fallback',
        title: '准备兜底策略',
        content: '如果模型等待过久或调用失败，会按保守规则继续识别，保证申请、报销和草稿流程还能推进。',
        status: 'running'
      }
    }
  ]
}
