#!/bin/bash
# verify-project-module.sh
# 验证 developing-project-management Skill 的完整性

SKILL_DIR="$(dirname "$0")/.."
REFERENCE_DIR="$SKILL_DIR/reference"

echo "========================================"
echo "验证 developing-project-management Skill"
echo "========================================"
echo ""

PASS_COUNT=0
FAIL_COUNT=0

pass() {
    echo "PASS: $1"
    PASS_COUNT=$((PASS_COUNT + 1))
}

fail() {
    echo "FAIL: $1"
    FAIL_COUNT=$((FAIL_COUNT + 1))
}

# ========================================
# 1. 验证章节分层目录结构
# ========================================
echo "--- 1. 验证章节分层目录结构 ---"

# 检查必需的章节目录
for chapter in "01-architecture-overview" "02-lifecycle-state-machine" "03-permission-model" "04-version-control" "05-database-schema" "06-api-design" "07-frontend-design"; do
    if [ -d "$REFERENCE_DIR/$chapter" ]; then
        pass "章节目录存在: $chapter"
    else
        fail "章节目录缺失: $chapter"
    fi
done

# ========================================
# 2. 验证 reference 文件包含 DDS 追溯信息
# ========================================
echo ""
echo "--- 2. 验证 DDS 追溯信息 ---"

# 检查 DDS-Section 标记
if grep -r "DDS-Section:" "$REFERENCE_DIR" > /dev/null 2>&1; then
    pass "存在 DDS-Section 追溯标记"
else
    fail "缺少 DDS-Section 追溯标记"
fi

# 检查 DDS-Lines 标记
if grep -r "DDS-Lines:" "$REFERENCE_DIR" > /dev/null 2>&1; then
    pass "存在 DDS-Lines 追溯标记"
else
    fail "缺少 DDS-Lines 追溯标记"
fi

# ========================================
# 3. 验证前端设计文档完整性
# ========================================
echo ""
echo "--- 3. 验证前端设计文档完整性 ---"

FRONTEND_DIR="$REFERENCE_DIR/07-frontend-design"

for file in "page-architecture.md" "view-edit-states.md" "user-admin-difference.md" "lifecycle-workflow-display.md" "module-design-specs.md" "component-specifications.md" "visual-design-specs.md" "interaction-sequences.md"; do
    if [ -f "$FRONTEND_DIR/$file" ]; then
        pass "前端设计文档存在: $file"
    else
        fail "前端设计文档缺失: $file"
    fi
done

# ========================================
# 4. 验证后端设计文档完整性
# ========================================
echo ""
echo "--- 4. 验证后端设计文档完整性 ---"

# 检查模块依赖文档
if [ -f "$REFERENCE_DIR/01-architecture-overview/module-dependencies.md" ]; then
    pass "模块依赖文档存在"
else
    fail "模块依赖文档缺失"
fi

# 检查状态机文档
if [ -f "$REFERENCE_DIR/02-lifecycle-state-machine/lifecycle-states.md" ]; then
    pass "生命周期状态文档存在"
else
    fail "生命周期状态文档缺失"
fi

# 检查工单映射文档
if [ -f "$REFERENCE_DIR/02-lifecycle-state-machine/workflow-state-mapping.md" ]; then
    pass "工单状态映射文档存在"
else
    fail "工单状态映射文档缺失"
fi

# 检查权限模型文档
if [ -f "$REFERENCE_DIR/03-permission-model/acl-permission.md" ]; then
    pass "ACL 权限模型文档存在"
else
    fail "ACL 权限模型文档缺失"
fi

# 检查版本控制文档
if [ -f "$REFERENCE_DIR/04-version-control/version-design.md" ]; then
    pass "版本控制设计文档存在"
else
    fail "版本控制设计文档缺失"
fi

# 检查数据库schema文档
if [ -f "$REFERENCE_DIR/05-database-schema/database-schema.md" ]; then
    pass "数据库 Schema 文档存在"
else
    fail "数据库 Schema 文档缺失"
fi

# 检查API文档
if [ -f "$REFERENCE_DIR/06-api-design/api-endpoints.md" ]; then
    pass "API 端点文档存在"
else
    fail "API 端点文档缺失"
fi

# ========================================
# 5. 验证 SKILL.md 结构
# ========================================
echo ""
echo "--- 5. 验证 SKILL.md 结构 ---"

SKILL_FILE="$SKILL_DIR/SKILL.md"

# 检查 frontmatter
if grep -q "^name:" "$SKILL_FILE" && grep -q "^description:" "$SKILL_FILE"; then
    pass "SKILL.md frontmatter 结构正确"
else
    fail "SKILL.md frontmatter 结构缺失"
fi

# 检查章节引用
if grep -q "reference/07-frontend-design/" "$SKILL_FILE"; then
    pass "SKILL.md 引用了前端设计文档"
else
    fail "SKILL.md 未引用前端设计文档"
fi

# 检查前端相关内容
if grep -q "Vue3\|Vuetify\|frontend" "$SKILL_FILE"; then
    pass "SKILL.md 包含前端开发指导"
else
    fail "SKILL.md 缺少前端开发指导"
fi

# 检查核心章节
for section in "Plan" "Verify" "Execute" "Pitfalls"; do
    if grep -q "## $section" "$SKILL_FILE"; then
        pass "SKILL.md 包含 $section 章节"
    else
        fail "SKILL.md 缺少 $section 章节"
    fi
done

# ========================================
# 总结
# ========================================
echo ""
echo "========================================"
echo "验证完成"
echo "========================================"
echo "PASS: $PASS_COUNT"
echo "FAIL: $FAIL_COUNT"
echo ""

if [ "$FAIL_COUNT" -eq 0 ]; then
    echo "所有验证通过!"
    exit 0
else
    echo "存在 $FAIL_COUNT 个验证失败项"
    exit 1
fi
