export const emptyDigitalEmployeeDashboard = {
  windowDays: 7,
  generatedAt: '',
  hasRealData: false,
  totals: {
    totalRuns: 0,
    successRuns: 0,
    failedRuns: 0,
    runningRuns: 0,
    toolCalls: 0,
    businessOutputs: 0,
    riskObservations: 0,
    riskClues: 0,
    profileSnapshots: 0,
    knowledgeDocuments: 0,
    successRate: 0,
    failureRate: 0
  },
  dailyWork: [],
  taskDistribution: [],
  categoryDistribution: [
    { name: '积累', value: 0, count: 0, color: 'var(--chart-blue)', description: '沉淀画像、基线和反馈样本' },
    { name: '升级', value: 0, count: 0, color: 'var(--chart-amber)', description: '输出待复核线索和优化建议' },
    { name: '整理', value: 0, count: 0, color: 'var(--success)', description: '整理制度、条款、知识和样本' },
    { name: '评估', value: 0, count: 0, color: 'var(--theme-primary)', description: '评估异常、风险和一致性' }
  ],
  recentRuns: []
}

export function buildDigitalEmployeeKpiMetrics(dashboard, formatNumberCompact) {
  const data = dashboard || emptyDigitalEmployeeDashboard
  const totals = data.totals || emptyDigitalEmployeeDashboard.totals
  const rows = [
    {
      label: '工作总数',
      value: formatNumberCompact(totals.totalRuns),
      changeText: `${data.windowDays || 7}天`,
      delta: '后台任务',
      trend: 'up',
      icon: 'mdi mdi-briefcase-clock-outline',
      accent: 'var(--theme-primary)'
    },
    {
      label: '成功数量',
      value: formatNumberCompact(totals.successRuns),
      changeText: `${Number(totals.successRate || 0).toFixed(1)}%`,
      delta: '运行成功率',
      trend: 'up',
      icon: 'mdi mdi-check-decagram-outline',
      accent: 'var(--success)'
    },
    {
      label: '失败数量',
      value: formatNumberCompact(totals.failedRuns),
      changeText: `${Number(totals.failureRate || 0).toFixed(1)}%`,
      delta: '需排查',
      trend: Number(totals.failedRuns || 0) > 0 ? 'down' : 'up',
      icon: 'mdi mdi-alert-circle-outline',
      accent: '#ef4444'
    },
    {
      label: '业务产出',
      value: formatNumberCompact(totals.businessOutputs),
      changeText: '累计',
      delta: '观察/线索/快照/文档',
      trend: 'up',
      icon: 'mdi mdi-chart-box-outline',
      accent: '#0f766e'
    },
    {
      label: '工具调用',
      value: formatNumberCompact(totals.toolCalls),
      changeText: '执行链',
      delta: '工具执行次数',
      trend: 'up',
      icon: 'mdi mdi-tools',
      accent: '#2563eb'
    },
    {
      label: '运行中',
      value: formatNumberCompact(totals.runningRuns),
      changeText: Number(totals.runningRuns || 0) > 0 ? '进行中' : '空闲',
      delta: '当前窗口',
      trend: Number(totals.runningRuns || 0) > 0 ? 'down' : 'up',
      icon: 'mdi mdi-progress-clock',
      accent: '#f59e0b'
    }
  ]

  return rows.map((item, index) => ({
    ...item,
    displayValue: item.value,
    delay: index * 55
  }))
}

export function buildDigitalEmployeeDailyRows(dashboard) {
  const rows = Array.isArray(dashboard?.dailyWork) ? dashboard.dailyWork : []
  return rows.map((item) => ({
    date: String(item.date || '').trim() || '-',
    total: Number(item.total || 0),
    success: Number(item.success || 0),
    failed: Number(item.failed || 0),
    running: Number(item.running || 0),
    riskObservations: Number(item.riskObservations || 0),
    riskClues: Number(item.riskClues || 0),
    profileSnapshots: Number(item.profileSnapshots || 0),
    knowledgeDocuments: Number(item.knowledgeDocuments || 0),
    businessOutputs: Number(item.businessOutputs || 0)
  }))
}

export function buildDigitalEmployeeTaskRanking(dashboard) {
  return (dashboard?.taskDistribution || [])
    .slice(0, 6)
    .map((item) => ({
      name: item.name,
      shortName: item.name,
      value: Number(item.value || item.count || 0),
      color: item.color || 'var(--theme-primary)'
    }))
}

export function buildDigitalEmployeeCategoryRows(dashboard) {
  return (dashboard?.categoryDistribution || [])
    .map((item) => ({
      ...item,
      value: Number(item.value || item.count || 0),
      count: Number(item.count || item.value || 0)
    }))
}
