#!/usr/bin/env sh
# 项目模板验证脚本

set -eu

echo "========================================"
echo "🧩 项目模板验证"
echo "========================================"

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PLAYBOOK_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
TEMPLATES_DIR="$PLAYBOOK_ROOT/templates"

VALIDATION_PASSED=0
VALIDATION_FAILED=0
ERRORS_FILE="/tmp/project_template_validation_errors.txt"
REPORT_FILE="$SCRIPT_DIR/project_templates_report.txt"

> "$ERRORS_FILE"
> "$REPORT_FILE"

echo "📁 模板目录: $TEMPLATES_DIR"
echo ""

# ============================================
# 辅助函数
# ============================================

validate_file_exists() {
    local file="$1"
    local description="$2"

    if [ -f "$file" ]; then
        echo "  ✅ $description: $(basename "$file")"
        VALIDATION_PASSED=$((VALIDATION_PASSED + 1))
        return 0
    else
        echo "  ❌ $description: $(basename "$file") - 文件不存在"
        echo "文件不存在: $file" >> "$ERRORS_FILE"
        VALIDATION_FAILED=$((VALIDATION_FAILED + 1))
        return 1
    fi
}

validate_contains() {
    local file="$1"
    local needle="$2"
    local description="$3"

    if grep -Fq "$needle" "$file"; then
        echo "    ✓ $description"
        VALIDATION_PASSED=$((VALIDATION_PASSED + 1))
    else
        echo "    ✗ $description"
        echo "缺少内容: $needle in $file" >> "$ERRORS_FILE"
        VALIDATION_FAILED=$((VALIDATION_FAILED + 1))
    fi
}

echo "🔍 验证核心模板文件"
AGENTS_TEMPLATE="$TEMPLATES_DIR/AGENTS.template.md"
AGENT_RULES_TEMPLATE="$TEMPLATES_DIR/AGENT_RULES.template.md"
README_TEMPLATE="$TEMPLATES_DIR/README.md"

if validate_file_exists "$AGENTS_TEMPLATE" "AGENTS.template.md"; then
    validate_contains "$AGENTS_TEMPLATE" "<!-- playbook:templates:start -->" "包含 templates 标记"
    validate_contains "$AGENTS_TEMPLATE" "<!-- playbook:framework:start -->" "包含 framework 标记"
    validate_contains "$AGENTS_TEMPLATE" "{{DATE}}" "包含 {{DATE}} 占位符"
fi

if validate_file_exists "$AGENT_RULES_TEMPLATE" "AGENT_RULES.template.md"; then
    validate_contains "$AGENT_RULES_TEMPLATE" "AGENT_RULES" "包含 AGENT_RULES 标题"
    validate_contains "$AGENT_RULES_TEMPLATE" "{{DATE}}" "包含 {{DATE}} 占位符"
fi

validate_file_exists "$README_TEMPLATE" "templates/README.md"

echo ""
echo "🔍 验证 memory-bank 模板"

MEMORY_BANK_DIR="$TEMPLATES_DIR/memory-bank"
for name in project-brief tech-stack architecture progress decisions; do
    validate_file_exists "$MEMORY_BANK_DIR/$name.template.md" "memory-bank/$name.template.md"
done

echo ""
echo "🔍 验证 prompts 模板"

PROMPTS_DIR="$TEMPLATES_DIR/prompts"
validate_file_exists "$PROMPTS_DIR/README.md" "prompts/README.md"
validate_file_exists "$PROMPTS_DIR/system/agent-behavior.template.md" "prompts/system/agent-behavior.template.md"
validate_file_exists "$PROMPTS_DIR/coding/clarify.template.md" "prompts/coding/clarify.template.md"
validate_file_exists "$PROMPTS_DIR/coding/review.template.md" "prompts/coding/review.template.md"
validate_file_exists "$PROMPTS_DIR/meta/prompt-generator.template.md" "prompts/meta/prompt-generator.template.md"

echo ""

# ============================================
# 生成验证报告
# ============================================

echo "========================================"
echo "📊 验证结果统计"
echo "========================================"
echo "✅ 通过: $VALIDATION_PASSED"
echo "❌ 失败: $VALIDATION_FAILED"

if [ $((VALIDATION_PASSED + VALIDATION_FAILED)) -gt 0 ]; then
    echo "📈 通过率: $(awk "BEGIN {printf \"%.1f\", ($VALIDATION_PASSED * 100.0) / ($VALIDATION_PASSED + $VALIDATION_FAILED)}")%"
else
    echo "📈 通过率: N/A (无测试项)"
fi

echo ""

{
    echo "项目模板验证报告"
    echo "===================="
    echo ""
    echo "验证时间: $(date '+%Y-%m-%d %H:%M:%S')"
    echo "模板目录: $TEMPLATES_DIR"
    echo ""
    echo "统计结果:"
    echo "  通过: $VALIDATION_PASSED"
    echo "  失败: $VALIDATION_FAILED"
    if [ $((VALIDATION_PASSED + VALIDATION_FAILED)) -gt 0 ]; then
        echo "  通过率: $(awk "BEGIN {printf \"%.1f\", ($VALIDATION_PASSED * 100.0) / ($VALIDATION_PASSED + $VALIDATION_FAILED)}")%"
    fi
    echo ""
    if [ -s "$ERRORS_FILE" ]; then
        echo "错误详情:"
        cat "$ERRORS_FILE"
    fi
} > "$REPORT_FILE"

echo "📄 详细报告: $REPORT_FILE"
echo "========================================"

rm -f "$ERRORS_FILE"

if [ "$VALIDATION_FAILED" -eq 0 ]; then
    echo "✅ 所有项目模板验证通过"
    exit 0
else
    echo "❌ 项目模板验证失败 ($VALIDATION_FAILED 个错误)"
    exit 1
fi
