#!/usr/bin/env bash
set -euo pipefail

SUMMARY_FILE="${1:?summary file required}"
PROJECT_DIR="${2:-/home/long/project/蚊子}"

[ -s "$SUMMARY_FILE" ] || { echo "summary missing/empty" >&2; exit 2; }

# 1) 必须包含结构化字段
for k in "执行命令" "修改文件" "测试结果" "剩余未完成"; do
  grep -q "$k" "$SUMMARY_FILE" || { echo "missing section: $k" >&2; exit 3; }
done

# 2) 至少有3条命令证据（bash代码块内或行内命令）
cmd_count=$(grep -E "(mvn|npm|npx|pnpm|yarn|pytest|go test|gradle|playwright)" "$SUMMARY_FILE" | wc -l | tr -d ' ')
if [ "${cmd_count:-0}" -lt 3 ]; then
  echo "insufficient command evidence: $cmd_count" >&2
  exit 4
fi

# 3) 提取日志路径并验证至少2个真实存在
# 允许绝对路径，或相对 logs/ target/ frontend/**/test-results/
mapfile -t paths < <(grep -Eo '/[^ )`"]+\.(log|txt|md|xml|json)|((logs|target|frontend)/[^ )`"]+\.(log|txt|md|xml|json))' "$SUMMARY_FILE" | sort -u)

exists=0
for p in "${paths[@]:-}"; do
  if [[ "$p" = /* ]]; then
    fp="$p"
  else
    fp="$PROJECT_DIR/$p"
  fi
  if [ -f "$fp" ]; then
    exists=$((exists+1))
  fi
done

if [ "$exists" -lt 2 ]; then
  echo "insufficient raw log path evidence: $exists" >&2
  exit 5
fi

echo "evidence ok: cmd_count=$cmd_count existing_logs=$exists"
