#!/usr/bin/env bash
# Comprehensive Project Update Script
# Updates all files to ensure consistency with current standards

set -euo pipefail

PROJECT_ROOT="/home/intlc/projects/proxmox"
PROXMOX_PROJECT="$PROJECT_ROOT/smom-dbis-138-proxmox"

echo "=== Comprehensive Project Update ==="
echo ""
echo "This script will:"
echo "  1. Verify VMID consistency (1000-1004, 1500-1503, 2500-2502)"
echo "  2. Check for outdated IP addresses (10.3.1.X should be 192.168.11.X)"
echo "  3. Verify Besu documentation references"
echo "  4. Check for old VMID references (106-122)"
echo ""

cd "$PROJECT_ROOT"

# Expected values
EXPECTED_VALIDATOR_COUNT=5
EXPECTED_VALIDATOR_RANGE="1000-1004"
EXPECTED_SENTRY_COUNT=4
EXPECTED_SENTRY_RANGE="1500-1503"
EXPECTED_RPC_COUNT=3
EXPECTED_RPC_RANGE="2500-2502"
EXPECTED_SUBNET="192.168.11"
OLD_SUBNET="10.3.1"
EXPECTED_GATEWAY="192.168.11.1"

errors=0
warnings=0

echo "=== 1. Checking VMID Ranges ==="
echo ""

# Check proxmox.conf for correct VMID ranges
if grep -q "VALIDATOR_COUNT=5" "$PROXMOX_PROJECT/config/proxmox.conf" 2>/dev/null; then
    echo "  ✅ VALIDATOR_COUNT=5 in proxmox.conf"
else
    echo "  ⚠️  VALIDATOR_COUNT not set to 5 in proxmox.conf"
    warnings=$((warnings + 1))
fi

if grep -q "SENTRY_COUNT=4" "$PROXMOX_PROJECT/config/proxmox.conf" 2>/dev/null; then
    echo "  ✅ SENTRY_COUNT=4 in proxmox.conf"
else
    echo "  ⚠️  SENTRY_COUNT not set to 4 in proxmox.conf"
    warnings=$((warnings + 1))
fi

if grep -q "RPC_COUNT=3" "$PROXMOX_PROJECT/config/proxmox.conf" 2>/dev/null; then
    echo "  ✅ RPC_COUNT=3 in proxmox.conf"
else
    echo "  ⚠️  RPC_COUNT not set to 3 in proxmox.conf"
    warnings=$((warnings + 1))
fi

echo ""
echo "=== 2. Checking IP Address References ==="
echo ""

# Check for old IP subnet
old_ips=$(grep -rE "\b10\.3\.1\." "$PROXMOX_PROJECT" \
    --include="*.sh" --include="*.md" --include="*.conf" \
    2>/dev/null | grep -v ".git" | grep -v "node_modules" | wc -l)

if [[ $old_ips -gt 0 ]]; then
    echo "  ⚠️  Found $old_ips references to old IP subnet (10.3.1.X)"
    echo "     These should be updated to 192.168.11.X"
    warnings=$((warnings + old_ips))
else
    echo "  ✅ No old IP subnet references found"
fi

# Check network.conf for correct gateway
if grep -q "GATEWAY=\"$EXPECTED_GATEWAY\"" "$PROXMOX_PROJECT/config/network.conf" 2>/dev/null; then
    echo "  ✅ Gateway correctly set to $EXPECTED_GATEWAY"
else
    echo "  ⚠️  Gateway may not be set to $EXPECTED_GATEWAY in network.conf"
    warnings=$((warnings + 1))
fi

echo ""
echo "=== 3. Checking Besu Documentation References ==="
echo ""

# Check for generic Besu documentation references
besu_docs=$(grep -rE "Besu.*[Dd]ocumentation|besu.*docs" "$PROXMOX_PROJECT" \
    --include="*.md" \
    2>/dev/null | grep -v "besu.hyperledger.org" | grep -v "github.com/hyperledger/besu" | wc -l)

if [[ $besu_docs -gt 0 ]]; then
    echo "  ⚠️  Found $besu_docs generic Besu documentation references"
    echo "     Should reference https://besu.hyperledger.org or https://github.com/hyperledger/besu"
    warnings=$((warnings + besu_docs))
else
    echo "  ✅ All Besu documentation references include official links"
fi

echo ""
echo "=== 4. Checking for Old VMID References ==="
echo ""

# Check for old VMID ranges (106-122)
old_vmids=$(grep -rE "\b(106|107|108|109|110|111|112|115|116|117|120|121|122)\b" "$PROXMOX_PROJECT" \
    --include="*.sh" --include="*.md" --include="*.conf" \
    2>/dev/null | grep -v ".git" | grep -v "node_modules" | \
    grep -v "1006\|1107\|2106" | grep -v "COMMENT\|#.*old" | wc -l)

if [[ $old_vmids -gt 0 ]]; then
    echo "  ⚠️  Found $old_vmids potential old VMID references"
    echo "     These should be reviewed to ensure they're not active references"
    warnings=$((warnings + old_vmids))
else
    echo "  ✅ No old VMID references found (or all are commented/contextual)"
fi

echo ""
echo "=== 5. Checking Validator Key Count ==="
echo ""

SOURCE_PROJECT="${SOURCE_PROJECT:-../smom-dbis-138}"
if [[ -d "$SOURCE_PROJECT/keys/validators" ]]; then
    key_count=$(find "$SOURCE_PROJECT/keys/validators" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | wc -l)
    if [[ $key_count -eq $EXPECTED_VALIDATOR_COUNT ]]; then
        echo "  ✅ Validator key count matches: $key_count"
    else
        echo "  ⚠️  Validator key count mismatch: found $key_count, expected $EXPECTED_VALIDATOR_COUNT"
        warnings=$((warnings + 1))
    fi
else
    echo "  ⚠️  Source project keys directory not found"
    warnings=$((warnings + 1))
fi

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

if [[ $errors -eq 0 && $warnings -eq 0 ]]; then
    echo "✅ Project appears to be fully updated and 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

