#!/usr/bin/env bash
# Check VMID consistency across all documentation and scripts
# Validates that all references match the expected ranges

set -euo pipefail

PROJECT_ROOT="/home/intlc/projects/proxmox"
cd "$PROJECT_ROOT"

# Expected VMID ranges
VALIDATORS_START=1000
VALIDATORS_END=1004
VALIDATORS_COUNT=5

SENTRIES_START=1500
SENTRIES_END=1503
SENTRIES_COUNT=4

RPC_START=2500
RPC_END=2502
RPC_COUNT=3

echo "=== VMID Consistency Check ==="
echo ""
echo "Expected Ranges:"
echo "  Validators: $VALIDATORS_START-$VALIDATORS_END ($VALIDATORS_COUNT nodes)"
echo "  Sentries: $SENTRIES_START-$SENTRIES_END ($SENTRIES_COUNT nodes)"
echo "  RPC: $RPC_START-$RPC_END ($RPC_COUNT nodes)"
echo ""

errors=0
warnings=0

# Check for validator VMID inconsistencies
echo "=== Checking Validator VMIDs ==="
echo ""

# Find all validator VMID references
validator_refs=$(grep -rE "\b(100[0-4]|1000-1004|validator.*VMID|VALIDATOR.*=)" \
    --include="*.md" --include="*.sh" --include="*.js" --include="*.py" \
    --include="*.conf" --include="*.example" \
    smom-dbis-138-proxmox/ 2>/dev/null | grep -v ".git" | cut -d: -f1 | sort -u)

if [[ -n "$validator_refs" ]]; then
    for file in $validator_refs; do
        # Check if file contains correct validator ranges
        if grep -qE "\b(1000-1004|1000.*1004|100[0-4])\b" "$file" 2>/dev/null; then
            # Check for incorrect validator ranges
            if grep -qE "\b(106|107|108|109|110|1110|1100-1104)\b" "$file" 2>/dev/null && ! grep -qE "\b(1000|1001|1002|1003|1004)\b" "$file" 2>/dev/null; then
                echo "  ❌ $file - Contains OLD validator VMIDs only"
                errors=$((errors + 1))
            elif grep -qE "\b(106|107|108|109|110)\b" "$file" 2>/dev/null; then
                echo "  ⚠️  $file - Contains BOTH old and new validator VMIDs"
                warnings=$((warnings + 1))
            else
                echo "  ✅ $file - Validator VMIDs look correct"
            fi
        fi
    done
fi

echo ""
echo "=== Checking Sentry VMIDs ==="
echo ""

# Find all sentry VMID references
sentry_refs=$(grep -rE "\b(150[0-3]|1500-1503|sentry.*VMID|SENTRY.*=)" \
    --include="*.md" --include="*.sh" --include="*.js" --include="*.py" \
    --include="*.conf" --include="*.example" \
    smom-dbis-138-proxmox/ 2>/dev/null | grep -v ".git" | cut -d: -f1 | sort -u)

if [[ -n "$sentry_refs" ]]; then
    for file in $sentry_refs; do
        # Check if file contains correct sentry ranges
        if grep -qE "\b(1500-1503|150[0-3])\b" "$file" 2>/dev/null; then
            # Check for incorrect sentry ranges
            if grep -qE "\b(111|112|113|114|1110|1110-1113)\b" "$file" 2>/dev/null && ! grep -qE "\b(1500|1501|1502|1503)\b" "$file" 2>/dev/null; then
                echo "  ❌ $file - Contains OLD sentry VMIDs only"
                errors=$((errors + 1))
            elif grep -qE "\b(111|112|113|114)\b" "$file" 2>/dev/null; then
                echo "  ⚠️  $file - Contains BOTH old and new sentry VMIDs"
                warnings=$((warnings + 1))
            else
                echo "  ✅ $file - Sentry VMIDs look correct"
            fi
        fi
    done
fi

echo ""
echo "=== Checking RPC VMIDs ==="
echo ""

# Find all RPC VMID references
rpc_refs=$(grep -rE "\b(250[0-2]|2500-2502|rpc.*VMID|RPC.*=)" \
    --include="*.md" --include="*.sh" --include="*.js" --include="*.py" \
    --include="*.conf" --include="*.example" \
    smom-dbis-138-proxmox/ 2>/dev/null | grep -v ".git" | cut -d: -f1 | sort -u)

if [[ -n "$rpc_refs" ]]; then
    for file in $rpc_refs; do
        # Check if file contains correct RPC ranges
        if grep -qE "\b(2500-2502|250[0-2])\b" "$file" 2>/dev/null; then
            # Check for incorrect RPC ranges
            if grep -qE "\b(115|116|117|1120|1120-1122)\b" "$file" 2>/dev/null && ! grep -qE "\b(2500|2501|2502)\b" "$file" 2>/dev/null; then
                echo "  ❌ $file - Contains OLD RPC VMIDs only"
                errors=$((errors + 1))
            elif grep -qE "\b(115|116|117)\b" "$file" 2>/dev/null; then
                echo "  ⚠️  $file - Contains BOTH old and new RPC VMIDs"
                warnings=$((warnings + 1))
            else
                echo "  ✅ $file - RPC VMIDs look correct"
            fi
        fi
    done
fi

echo ""
echo "=== Checking Count Consistency ==="
echo ""

# Check for count mismatches
validator_count_refs=$(grep -rE "(VALIDATOR.*COUNT|validators?.*count|5.*validator)" \
    --include="*.md" --include="*.sh" --include="*.conf" \
    smom-dbis-138-proxmox/ 2>/dev/null | grep -v ".git")

sentry_count_refs=$(grep -rE "(SENTRY.*COUNT|sentries?.*count|4.*sentry)" \
    --include="*.md" --include="*.sh" --include="*.conf" \
    smom-dbis-138-proxmox/ 2>/dev/null | grep -v ".git")

rpc_count_refs=$(grep -rE "(RPC.*COUNT|rpc.*count|3.*rpc)" \
    --include="*.md" --include="*.sh" --include="*.conf" \
    smom-dbis-138-proxmox/ 2>/dev/null | grep -v ".git")

# Check validator counts
for line in $validator_count_refs; do
    file=$(echo "$line" | cut -d: -f1)
    content=$(echo "$line" | cut -d: -f2-)
    if echo "$content" | grep -qE "\b(4|3|6|7|8)\b" && echo "$content" | grep -qi validator; then
        if ! echo "$content" | grep -qE "\b($VALIDATORS_COUNT|5)\b"; then
            echo "  ⚠️  $file - Validator count may be incorrect: $content"
            warnings=$((warnings + 1))
        fi
    fi
done

# Check sentry counts
for line in $sentry_count_refs; do
    file=$(echo "$line" | cut -d: -f1)
    content=$(echo "$line" | cut -d: -f2-)
    if echo "$content" | grep -qE "\b(3|5|6|7|8)\b" && echo "$content" | grep -qi sentry; then
        if ! echo "$content" | grep -qE "\b($SENTRIES_COUNT|4)\b"; then
            echo "  ⚠️  $file - Sentry count may be incorrect: $content"
            warnings=$((warnings + 1))
        fi
    fi
done

# Check RPC counts
for line in $rpc_count_refs; do
    file=$(echo "$line" | cut -d: -f1)
    content=$(echo "$line" | cut -d: -f2-)
    if echo "$content" | grep -qE "\b(2|4|5|6)\b" && echo "$content" | grep -qi rpc; then
        if ! echo "$content" | grep -qE "\b($RPC_COUNT|3)\b"; then
            echo "  ⚠️  $file - RPC count may be incorrect: $content"
            warnings=$((warnings + 1))
        fi
    fi
done

echo ""
echo "=== Checking Array Definitions ==="
echo ""

# Check for hardcoded VMID arrays
array_files=$(grep -rE "(VALIDATORS|SENTRIES|RPCS?)=\(.*\)" \
    --include="*.sh" --include="*.py" \
    smom-dbis-138-proxmox/ 2>/dev/null | cut -d: -f1 | sort -u)

for file in $array_files; do
    echo "  Checking: $file"
    # Check validators array
    if grep -qE "VALIDATORS.*=" "$file" 2>/dev/null; then
        validator_array=$(grep -A 1 "VALIDATORS.*=" "$file" 2>/dev/null | grep -E "\(.*\)")
        if echo "$validator_array" | grep -qE "\b(106|107|108|109|110)\b" && ! echo "$validator_array" | grep -qE "\b(1000|1001|1002|1003|1004)\b"; then
            echo "    ❌ Validators array contains old VMIDs: $validator_array"
            errors=$((errors + 1))
        fi
    fi
    
    # Check sentries array
    if grep -qE "SENTRIES.*=" "$file" 2>/dev/null; then
        sentry_array=$(grep -A 1 "SENTRIES.*=" "$file" 2>/dev/null | grep -E "\(.*\)")
        if echo "$sentry_array" | grep -qE "\b(111|112|113|114)\b" && ! echo "$sentry_array" | grep -qE "\b(1500|1501|1502|1503)\b"; then
            echo "    ❌ Sentries array contains old VMIDs: $sentry_array"
            errors=$((errors + 1))
        fi
    fi
    
    # Check RPC array
    if grep -qE "RPCS?.*=" "$file" 2>/dev/null; then
        rpc_array=$(grep -A 1 "RPCS?.*=" "$file" 2>/dev/null | grep -E "\(.*\)")
        if echo "$rpc_array" | grep -qE "\b(115|116|117)\b" && ! echo "$rpc_array" | grep -qE "\b(2500|2501|2502)\b"; then
            echo "    ❌ RPC array contains old VMIDs: $rpc_array"
            errors=$((errors + 1))
        fi
    fi
done

echo ""
echo "=== Summary ==="
echo ""
echo "Errors found: $errors"
echo "Warnings found: $warnings"
echo ""

if [[ $errors -eq 0 && $warnings -eq 0 ]]; then
    echo "✅ All VMID references appear consistent!"
    exit 0
elif [[ $errors -eq 0 ]]; then
    echo "⚠️  Some warnings found - review recommended"
    exit 0
else
    echo "❌ Errors found - fix required"
    exit 1
fi

