/**
 * Command: status — 显示 manifest 执行状态
 */

const { loadManifest } = require('./pipeline-utils')

const ALL_PHASES = ['images', 'upload', 'videos', 'tts', 'assemble']

function showStatus(manifestPath) {
  const manifest = loadManifest(manifestPath)
  const phases = manifest.pipeline?.phases || {}

  console.log(`\nManifest: ${manifestPath}`)
  console.log(`Account: ${manifest.account || '(未指定)'}`)
  console.log(`\n阶段状态:`)
  for (const p of ALL_PHASES) {
    const status = phases[p] || 'pending'
    const icon = status === 'done' ? '✓' : status === 'running' ? '→' : status === 'failed' ? '✗' : status === 'partial' ? '~' : '·'
    console.log(`  ${icon} ${p}: ${status}`)
  }

  const items = manifest.items || []
  const done = items.filter(it => it.status === 'done').length
  const failed = items.filter(it => it.status === 'failed').length
  const pending = items.filter(it => !it.status || it.status === 'pending').length

  console.log(`\nItems: ${items.length} 总计, ${done} 完成, ${failed} 失败, ${pending} 待处理`)

  if (failed > 0) {
    console.log(`\n失败项:`)
    items.filter(it => it.status === 'failed').forEach((it, i) => {
      console.log(`  [${it.id || i + 1}] ${it.error || '未知错误'}`)
    })
  }
  console.log()
}

module.exports = { showStatus }
