#!/bin/bash
# verify-module-deps.sh - 验证模块依赖一致性
# 依赖: go, grep
# 用法: ./verify-module-deps.sh

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="${SCRIPT_DIR}/../../.."

echo "=== RMDC 模块依赖验证 ==="
echo ""

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

pass() { echo -e "${GREEN}[PASS]${NC} $1"; }
fail() { echo -e "${RED}[FAIL]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }

# 检查 go.mod 中的依赖版本一致性
check_common_version() {
    echo "--- 检查 rmdc-common 版本一致性 ---"

    VERSIONS=$(find "${PROJECT_ROOT}" -name "go.mod" -exec grep "rmdc-common" {} \; 2>/dev/null | \
        grep -oE "v[0-9]+\.[0-9]+\.[0-9]+" | sort | uniq)

    VERSION_COUNT=$(echo "$VERSIONS" | wc -l)

    if [ "$VERSION_COUNT" -eq 1 ]; then
        pass "rmdc-common 版本一致: $VERSIONS"
    else
        fail "rmdc-common 版本不一致: $VERSIONS"
    fi
}

# 检查循环依赖
check_circular_deps() {
    echo "--- 检查循环依赖 ---"

    cd "${PROJECT_ROOT}"
    if go mod graph 2>/dev/null | grep -E "rmdc.*rmdc" | head -20; then
        warn "发现模块间依赖，请确认无循环"
    else
        pass "未发现明显循环依赖"
    fi
}

# 执行检查
check_common_version
check_circular_deps

echo ""
echo "=== 依赖验证完成 ==="
