#!/usr/bin/env bash
# archive-progress.sh — UAT 签字后归档 progress/ 到 docs/qa/<feature>-process-log.md
#
# 用法:
#   bash .claude/scripts/archive-progress.sh <feature>
#
# 行为:
#   1. cat progress/*.md (除 README.md) > docs/qa/<feature>-process-log.md
#   2. git rm progress/*.md（保留 .gitkeep + README.md）
#
# 调用方: product-lead，UAT 业务签字后执行（Self-Reporting Pattern 闭环）。
# 前置: 所有执行层 teammate 已停止写入 progress/，避免 race condition。

set -euo pipefail

FEATURE="${1:-}"
if [ -z "$FEATURE" ]; then
  echo "Usage: $0 <feature>  (e.g. login-2026-04-24, must match docs/prd/<feature>.md)" >&2
  exit 1
fi

OUT="docs/qa/${FEATURE}-process-log.md"

{
  echo "# Process Log: $FEATURE"
  echo ""
  echo "归档自 progress/*.md，UAT 签字后由 product-lead 写入。"
  echo "原始底稿对应每个执行层 teammate 在 feature 期间的完成/阻塞条目。"
  echo ""
  for f in progress/*.md; do
    [ "$(basename "$f")" = "README.md" ] && continue
    [ -e "$f" ] || continue
    echo "---"
    echo ""
    echo "## Source: $(basename "$f")"
    echo ""
    cat "$f"
    echo ""
  done
} > "$OUT"

echo "Wrote: $OUT"

git rm progress/*.md 2>/dev/null || true
git restore --staged --worktree progress/README.md 2>/dev/null || true

echo "progress/*.md removed (README.md + .gitkeep retained)."
