/**
 * Command: confirm — 人工确认分镜图
 */

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

function confirmManifest(options) {
  const { manifest: manifestPath, all, items: itemsStr } = options

  if (!manifestPath) {
    console.error('用法: pipeline.js confirm --manifest <path> --all')
    console.error('     pipeline.js confirm --manifest <path> --items 1,3,5')
    process.exit(1)
  }
  if (!all && !itemsStr) {
    console.error('错误: 必须指定 --all 或 --items <id列表>')
    process.exit(1)
  }

  const manifest = loadManifest(manifestPath)
  const targetIds = itemsStr
    ? new Set(itemsStr.split(',').map(s => parseInt(s.trim(), 10)).filter(n => !isNaN(n)))
    : null

  let count = 0
  for (const item of manifest.items) {
    if (targetIds && !targetIds.has(item.id)) continue
    if (item.file && item.status === 'done' && !item.confirmed) {
      item.confirmed = true
      count++
    }
  }

  saveManifest(manifestPath, manifest)

  const total = manifest.items.length
  const confirmed = manifest.items.filter(it => it.confirmed).length
  const scope = targetIds ? `${Array.from(targetIds).join(',')}` : '全部'
  console.log(`已确认: ${count} items（范围: ${scope}，共 ${confirmed}/${total} 已确认）`)
}

module.exports = { confirmManifest }
