#!/bin/bash
# verify-module-deps.sh
# Verifies RMDC module dependency rules
#
# Dependencies:
# - bash 4.0+
# - grep
# - find
#
# Usage:
#   ./verify-module-deps.sh [module-path]
#   ./verify-module-deps.sh /path/to/rmdc-project-management

set -e

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

MODULE_PATH="${1:-.}"
ERRORS=0
WARNINGS=0

echo "=========================================="
echo "RMDC Module Dependency Verification"
echo "=========================================="
echo "Checking: $MODULE_PATH"
echo ""

# Define prohibited dependencies
declare -A PROHIBITED_DEPS
PROHIBITED_DEPS["rmdc-jenkins-branch-dac"]="rmdc-project-management rmdc-exchange-hub rmdc-watchdog rmdc-user-auth"
PROHIBITED_DEPS["rmdc-project-management"]="rmdc-jenkins-branch-dac rmdc-exchange-hub rmdc-watchdog rmdc-user-auth"
PROHIBITED_DEPS["rmdc-exchange-hub"]="rmdc-jenkins-branch-dac rmdc-watchdog rmdc-user-auth"
PROHIBITED_DEPS["rmdc-watchdog"]="rmdc-jenkins-branch-dac rmdc-project-management rmdc-exchange-hub rmdc-user-auth rmdc-audit-log"
PROHIBITED_DEPS["rmdc-user-auth"]="rmdc-jenkins-branch-dac rmdc-project-management rmdc-exchange-hub rmdc-watchdog"
PROHIBITED_DEPS["rmdc-audit-log"]="rmdc-jenkins-branch-dac rmdc-project-management rmdc-exchange-hub rmdc-watchdog rmdc-user-auth"

# Detect current module
detect_module() {
    if [ -f "$MODULE_PATH/go.mod" ]; then
        grep "^module" "$MODULE_PATH/go.mod" | awk '{print $2}' | xargs basename
    else
        basename "$MODULE_PATH"
    fi
}

CURRENT_MODULE=$(detect_module)
echo "Detected module: $CURRENT_MODULE"
echo ""

# Check 1: Verify go.mod exists
echo "[Check 1] go.mod existence..."
if [ -f "$MODULE_PATH/go.mod" ]; then
    echo -e "${GREEN}PASS${NC}: go.mod found"
else
    echo -e "${RED}FAIL${NC}: go.mod not found"
    ((ERRORS++))
fi
echo ""

# Check 2: Verify prohibited imports in go.mod
echo "[Check 2] Prohibited dependencies in go.mod..."
if [ -f "$MODULE_PATH/go.mod" ]; then
    PROHIBITED="${PROHIBITED_DEPS[$CURRENT_MODULE]:-}"
    if [ -n "$PROHIBITED" ]; then
        for dep in $PROHIBITED; do
            if grep -q "$dep" "$MODULE_PATH/go.mod" 2>/dev/null; then
                echo -e "${RED}FAIL${NC}: Prohibited dependency found: $dep"
                ((ERRORS++))
            fi
        done
        if [ $ERRORS -eq 0 ]; then
            echo -e "${GREEN}PASS${NC}: No prohibited dependencies in go.mod"
        fi
    else
        echo -e "${YELLOW}SKIP${NC}: No prohibition rules for $CURRENT_MODULE"
    fi
else
    echo -e "${YELLOW}SKIP${NC}: go.mod not found"
fi
echo ""

# Check 3: Verify import statements in Go files
echo "[Check 3] Prohibited imports in source code..."
if [ -d "$MODULE_PATH" ]; then
    PROHIBITED="${PROHIBITED_DEPS[$CURRENT_MODULE]:-}"
    if [ -n "$PROHIBITED" ]; then
        for dep in $PROHIBITED; do
            FOUND=$(find "$MODULE_PATH" -name "*.go" -exec grep -l "\".*$dep" {} \; 2>/dev/null | head -5)
            if [ -n "$FOUND" ]; then
                echo -e "${RED}FAIL${NC}: Found import of $dep in:"
                echo "$FOUND" | while read -r f; do echo "  - $f"; done
                ((ERRORS++))
            fi
        done
        if [ $ERRORS -eq 0 ]; then
            echo -e "${GREEN}PASS${NC}: No prohibited imports in source files"
        fi
    else
        echo -e "${YELLOW}SKIP${NC}: No prohibition rules for $CURRENT_MODULE"
    fi
fi
echo ""

# Check 4: Verify standard directory structure
echo "[Check 4] Standard directory structure..."
REQUIRED_DIRS=("cmd" "internal" "configs")
OPTIONAL_DIRS=("pkg" "scripts" "docs")

for dir in "${REQUIRED_DIRS[@]}"; do
    if [ -d "$MODULE_PATH/$dir" ]; then
        echo -e "${GREEN}PASS${NC}: Required directory exists: $dir/"
    else
        echo -e "${RED}FAIL${NC}: Missing required directory: $dir/"
        ((ERRORS++))
    fi
done

for dir in "${OPTIONAL_DIRS[@]}"; do
    if [ -d "$MODULE_PATH/$dir" ]; then
        echo -e "${GREEN}INFO${NC}: Optional directory exists: $dir/"
    fi
done
echo ""

# Check 5: Verify internal structure
echo "[Check 5] Internal package structure..."
INTERNAL_DIRS=("config" "dao" "handler" "model" "service")
if [ -d "$MODULE_PATH/internal" ]; then
    for dir in "${INTERNAL_DIRS[@]}"; do
        if [ -d "$MODULE_PATH/internal/$dir" ]; then
            echo -e "${GREEN}PASS${NC}: internal/$dir/ exists"
        else
            echo -e "${YELLOW}WARN${NC}: internal/$dir/ not found"
            ((WARNINGS++))
        fi
    done
else
    echo -e "${YELLOW}SKIP${NC}: internal/ directory not found"
fi
echo ""

# Check 6: Verify rmdc-common dependency
echo "[Check 6] rmdc-common dependency..."
if [ -f "$MODULE_PATH/go.mod" ]; then
    if grep -q "rmdc-common" "$MODULE_PATH/go.mod"; then
        echo -e "${GREEN}PASS${NC}: rmdc-common dependency found"
    else
        echo -e "${YELLOW}WARN${NC}: rmdc-common dependency not found (may be acceptable for some modules)"
        ((WARNINGS++))
    fi
fi
echo ""

# Summary
echo "=========================================="
echo "Verification Summary"
echo "=========================================="
echo -e "Errors:   ${RED}$ERRORS${NC}"
echo -e "Warnings: ${YELLOW}$WARNINGS${NC}"
echo ""

if [ $ERRORS -gt 0 ]; then
    echo -e "${RED}VERIFICATION FAILED${NC}"
    exit 1
else
    echo -e "${GREEN}VERIFICATION PASSED${NC}"
    exit 0
fi
