#!/bin/bash
# verify.sh - dds-to-skill Skill 自身结构验证
#
# 验证本 Skill 的文件结构和内容完整性
# 用法：cd dds-to-skill && ./scripts/verify.sh

set -e

PASS=0; FAIL=0

check() {
  if eval "$2"; then
    echo "✅ PASS: $1"; ((PASS++))
  else
    echo "❌ FAIL: $1"; ((FAIL++))
  fi
}

SKILL_DIR="$(cd "$(dirname "$0")/.." && pwd)"

echo "=== dds-to-skill Skill 自检 ==="
echo "目录: $SKILL_DIR"
echo ""

# 结构检查
check "SKILL.md 存在" "test -f '$SKILL_DIR/SKILL.md'"
check "reference/ 目录存在" "test -d '$SKILL_DIR/reference'"
check "examples/ 目录存在" "test -d '$SKILL_DIR/examples'"
check "scripts/ 目录存在" "test -d '$SKILL_DIR/scripts'"

# SKILL.md 内容检查
check "SKILL.md < 500 行" "[ \$(wc -l < '$SKILL_DIR/SKILL.md') -lt 500 ]"
check "包含 name 字段" "head -20 '$SKILL_DIR/SKILL.md' | grep -q '^name:'"
check "包含 description 字段" "head -20 '$SKILL_DIR/SKILL.md' | grep -q '^description:'"
check "包含 argument-hint" "head -20 '$SKILL_DIR/SKILL.md' | grep -q 'argument-hint:'"

# 阶段结构检查
check "包含 Phase 0（读取）" "grep -q 'Phase 0' '$SKILL_DIR/SKILL.md'"
check "包含 Phase 1（分析）" "grep -q 'Phase 1' '$SKILL_DIR/SKILL.md'"
check "包含 Phase 2（抽取）" "grep -q 'Phase 2' '$SKILL_DIR/SKILL.md'"
check "包含 Phase 3（生成）" "grep -q 'Phase 3' '$SKILL_DIR/SKILL.md'"
check "包含 Phase 4（支撑文件）" "grep -q 'Phase 4' '$SKILL_DIR/SKILL.md'"
check "包含 Phase 5（自检）" "grep -q 'Phase 5' '$SKILL_DIR/SKILL.md'"

# Reference 文件检查
check "dds-extraction-guide.md 存在" "test -f '$SKILL_DIR/reference/dds-extraction-guide.md'"
check "skill-templates.md 存在" "test -f '$SKILL_DIR/reference/skill-templates.md'"
check "frontmatter-spec.md 存在" "test -f '$SKILL_DIR/reference/frontmatter-spec.md'"
check "quality-checklist.md 存在" "test -f '$SKILL_DIR/reference/quality-checklist.md'"

# Examples 检查
check "至少 1 个转换示例" "find '$SKILL_DIR/examples' -name '*.md' | grep -q ."

# 动态注入检查
INJECT_COUNT=$(grep -c '!\`' "$SKILL_DIR/SKILL.md" 2>/dev/null || echo 0)
check "SKILL.md 包含动态注入 (>= 2 处)" "[ $INJECT_COUNT -ge 2 ]"

echo ""
echo "=== 结果: $PASS PASS / $FAIL FAIL ==="
[ $FAIL -eq 0 ] && exit 0 || exit 1
