#!/bin/bash
# verify-skill-output.sh
# 验证 DDS-to-Skill 转换输出的完整性和质量
#
# 用法：./verify-skill-output.sh <skills-output-dir>
# 示例：./verify-skill-output.sh /path/to/1-AgentSkills
#
# 依赖：bash, grep, sed, find, wc

set -e

SKILLS_DIR="${1:-.}"
PASS=0
FAIL=0
WARN=0

# 颜色输出
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

pass() {
  echo -e "${GREEN}✅ PASS${NC}: $1"
  ((PASS++))
}

fail() {
  echo -e "${RED}❌ FAIL${NC}: $1"
  echo -e "   ${RED}修复${NC}: $2"
  ((FAIL++))
}

warn() {
  echo -e "${YELLOW}⚠️  WARN${NC}: $1"
  ((WARN++))
}

echo "============================================"
echo "  DDS-to-Skill 输出质量验证"
echo "  目标目录: $SKILLS_DIR"
echo "============================================"
echo ""

# ============================================
# 1. 结构完整性检查
# ============================================
echo "--- 1. 结构完整性 ---"

# S1: 检查是否有系统级 Skill
SYSTEM_SKILLS=$(find "$SKILLS_DIR" -maxdepth 1 -type d -name "*-system*" 2>/dev/null | wc -l)
if [ "$SYSTEM_SKILLS" -ge 1 ]; then
  pass "S1: 存在系统级 Skill ($SYSTEM_SKILLS 个)"
else
  warn "S1: 未找到系统级 Skill（名称包含 '-system'）"
fi

# S4: 每个 Skill 都有 SKILL.md
SKILL_DIRS=$(find "$SKILLS_DIR" -maxdepth 1 -type d ! -name "$(basename "$SKILLS_DIR")" 2>/dev/null)
MISSING_SKILLMD=0
for dir in $SKILL_DIRS; do
  if [ ! -f "$dir/SKILL.md" ]; then
    fail "S4: $dir 缺少 SKILL.md" "创建该目录下的 SKILL.md"
    ((MISSING_SKILLMD++))
  fi
done
if [ "$MISSING_SKILLMD" -eq 0 ]; then
  pass "S4: 所有 Skill 目录都有 SKILL.md"
fi

# S5: 每个 Skill 都有 reference/
MISSING_REF=0
for dir in $SKILL_DIRS; do
  if [ ! -d "$dir/reference" ]; then
    warn "S5: $dir 缺少 reference/ 目录"
    ((MISSING_REF++))
  fi
done
if [ "$MISSING_REF" -eq 0 ]; then
  pass "S5: 所有 Skill 目录都有 reference/"
fi

echo ""

# ============================================
# 2. Frontmatter 规范检查
# ============================================
echo "--- 2. Frontmatter 规范 ---"

for dir in $SKILL_DIRS; do
  SKILL_FILE="$dir/SKILL.md"
  [ ! -f "$SKILL_FILE" ] && continue
  SKILL_NAME=$(basename "$dir")

  # F1: name 字段
  if head -20 "$SKILL_FILE" | grep -q '^name:'; then
    pass "F1 [$SKILL_NAME]: frontmatter 包含 name"
  else
    fail "F1 [$SKILL_NAME]: 缺少 name 字段" "在 frontmatter 中添加 name 字段"
  fi

  # F2: description 字段
  if head -20 "$SKILL_FILE" | grep -q '^description:'; then
    pass "F2 [$SKILL_NAME]: frontmatter 包含 description"
  else
    fail "F2 [$SKILL_NAME]: 缺少 description 字段" "在 frontmatter 中添加 description 字段"
  fi

  # C1: 行数 < 500
  LINE_COUNT=$(wc -l < "$SKILL_FILE")
  if [ "$LINE_COUNT" -lt 500 ]; then
    pass "C1 [$SKILL_NAME]: SKILL.md = $LINE_COUNT 行 (< 500)"
  else
    fail "C1 [$SKILL_NAME]: SKILL.md = $LINE_COUNT 行 (>= 500)" "将冗长内容移到 reference/ 中"
  fi
done

echo ""

# ============================================
# 3. 内容质量检查
# ============================================
echo "--- 3. 内容质量 ---"

for dir in $SKILL_DIRS; do
  SKILL_FILE="$dir/SKILL.md"
  [ ! -f "$SKILL_FILE" ] && continue
  SKILL_NAME=$(basename "$dir")

  # C2-C5: 必须章节
  for section in "Plan" "Verify" "Execute" "Pitfalls"; do
    if grep -q "## $section" "$SKILL_FILE"; then
      pass "C [$SKILL_NAME]: 包含 ## $section"
    else
      warn "C [$SKILL_NAME]: 缺少 ## $section 章节"
    fi
  done

  # C6: 动态注入
  INJECT_COUNT=$(grep -c '!`' "$SKILL_FILE" 2>/dev/null || echo 0)
  if [ "$INJECT_COUNT" -ge 2 ]; then
    pass "C6 [$SKILL_NAME]: $INJECT_COUNT 处动态注入 (>= 2)"
  else
    warn "C6 [$SKILL_NAME]: 仅 $INJECT_COUNT 处动态注入 (建议 >= 2)"
  fi

  # C7: Pitfalls 引用 reference
  REF_IN_PITFALLS=$(sed -n '/## Pitfalls/,/## /p' "$SKILL_FILE" | grep -c 'reference/' 2>/dev/null || echo 0)
  if [ "$REF_IN_PITFALLS" -ge 2 ]; then
    pass "C7 [$SKILL_NAME]: Pitfalls 中 $REF_IN_PITFALLS 处引用 reference (>= 2)"
  else
    warn "C7 [$SKILL_NAME]: Pitfalls 中仅 $REF_IN_PITFALLS 处引用 reference (建议 >= 2)"
  fi
done

echo ""

# ============================================
# 4. Reference 质量检查
# ============================================
echo "--- 4. Reference 质量 ---"

for dir in $SKILL_DIRS; do
  [ ! -d "$dir/reference" ] && continue
  SKILL_NAME=$(basename "$dir")

  # R2: 章节分层
  SECTION_DIRS=$(find "$dir/reference" -maxdepth 1 -type d -name '0*' 2>/dev/null | wc -l)
  if [ "$SECTION_DIRS" -ge 1 ]; then
    pass "R2 [$SKILL_NAME]: reference 有 $SECTION_DIRS 个章节子目录"
  else
    warn "R2 [$SKILL_NAME]: reference 无章节子目录（使用扁平结构）"
  fi

  # R3: DDS 溯源
  DDS_SECTION_COUNT=$(grep -r 'DDS-Section:' "$dir/reference/" 2>/dev/null | wc -l)
  if [ "$DDS_SECTION_COUNT" -ge 1 ]; then
    pass "R3 [$SKILL_NAME]: $DDS_SECTION_COUNT 处 DDS-Section 溯源"
  else
    warn "R3 [$SKILL_NAME]: 无 DDS-Section 溯源标记"
  fi

  # R5: TBD 标注
  TBD_COUNT=$(grep -r '\[TBD' "$dir/reference/" 2>/dev/null | wc -l)
  if [ "$TBD_COUNT" -ge 0 ]; then
    pass "R5 [$SKILL_NAME]: $TBD_COUNT 处 [TBD] 标注"
  fi

  # R1: 设计要素类型数
  REF_FILES=$(find "$dir/reference" -name "*.md" 2>/dev/null | wc -l)
  if [ "$REF_FILES" -ge 3 ]; then
    pass "R1 [$SKILL_NAME]: $REF_FILES 个 reference 文件 (>= 3)"
  else
    warn "R1 [$SKILL_NAME]: 仅 $REF_FILES 个 reference 文件 (建议 >= 3)"
  fi
done

echo ""

# ============================================
# 总结
# ============================================
echo "============================================"
echo "  验证完成"
echo "  ✅ PASS: $PASS"
echo "  ❌ FAIL: $FAIL"
echo "  ⚠️  WARN: $WARN"
echo "============================================"

if [ "$FAIL" -gt 0 ]; then
  exit 1
else
  exit 0
fi
