#!/usr/bin/env bash
# Review consistency between proxmox deployment project and source smom-dbis-138 project

set -euo pipefail

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

echo "=== Cross-Project Consistency Review ==="
echo ""
echo "Source Project: $SOURCE_PROJECT"
echo "Proxmox Project: $PROXMOX_PROJECT"
echo ""

if [[ ! -d "$SOURCE_PROJECT" ]]; then
    echo "❌ Source project directory not found: $SOURCE_PROJECT"
    exit 1
fi

if [[ ! -d "$PROXMOX_PROJECT" ]]; then
    echo "❌ Proxmox project directory not found: $PROXMOX_PROJECT"
    exit 1
fi

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

# Expected IP ranges
EXPECTED_SUBNET="192.168.11"
OLD_SUBNET="10.3.1"

# Check source project for IP references
echo "Searching for IP address references in source project..."
SOURCE_IPS=$(grep -rE "(10\.3\.1\.|192\.168\.11\.)" "$SOURCE_PROJECT" \
    --include="*.md" --include="*.sh" --include="*.js" --include="*.py" \
    --include="*.toml" --include="*.json" --include="*.conf" \
    2>/dev/null | grep -v node_modules | grep -v ".git" | head -20 || true)

if [[ -n "$SOURCE_IPS" ]]; then
    echo "⚠️  IP addresses found in source project:"
    echo "$SOURCE_IPS" | head -10
else
    echo "✅ No IP address references found (or none in searched file types)"
fi

echo ""
echo "=== 2. Checking VMID References ==="
echo ""

# Check for VMID references in source project
SOURCE_VMIDS=$(grep -rE "\b(VMID|vmid)\b" "$SOURCE_PROJECT" \
    --include="*.md" --include="*.sh" \
    2>/dev/null | grep -v node_modules | grep -v ".git" | head -20 || true)

if [[ -n "$SOURCE_VMIDS" ]]; then
    echo "⚠️  VMID references found in source project:"
    echo "$SOURCE_VMIDS" | head -10
else
    echo "✅ No VMID references found in source project (expected - it's deployment-specific)"
fi

echo ""
echo "=== 3. Checking Network Configuration Files ==="
echo ""

# Check for network config files
NETWORK_FILES=(
    "$SOURCE_PROJECT/config/genesis.json"
    "$SOURCE_PROJECT/config/permissions-nodes.toml"
    "$SOURCE_PROJECT/config/permissions-accounts.toml"
    "$SOURCE_PROJECT/config/config-validator.toml"
    "$SOURCE_PROJECT/config/config-sentry.toml"
    "$SOURCE_PROJECT/config/config-rpc*.toml"
)

echo "Checking for Besu configuration files in source project:"
for file in "${NETWORK_FILES[@]}"; do
    if [[ -f "$file" ]] || ls "$file" 2>/dev/null | grep -q .; then
        echo "  ✅ Found: $(basename "$file" 2>/dev/null || echo "$file")"
    else
        echo "  ⚠️  Not found: $(basename "$file" 2>/dev/null || echo "$file")"
    fi
done

echo ""
echo "=== 4. Checking Validator Keys ==="
echo ""

KEYS_DIR="$SOURCE_PROJECT/keys/validators"
if [[ -d "$KEYS_DIR" ]]; then
    KEY_COUNT=$(find "$KEYS_DIR" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | wc -l)
    echo "  ✅ Validator keys directory exists: $KEYS_DIR"
    echo "  Found $KEY_COUNT validator key directories"
    
    if [[ $KEY_COUNT -ne 5 ]]; then
        echo "  ⚠️  Expected 5 validators, found $KEY_COUNT"
    fi
else
    echo "  ⚠️  Validator keys directory not found: $KEYS_DIR"
fi

echo ""
echo "=== 5. Checking Chain ID Consistency ==="
echo ""

CHAIN_ID_PROXMOX=$(grep -rE "CHAIN_ID|chain.?id|chainId" "$PROXMOX_PROJECT/config" \
    --include="*.conf" --include="*.toml" \
    2>/dev/null | grep -iE "138" | head -1 || echo "")

CHAIN_ID_SOURCE=$(grep -rE "chain.?id|chainId" "$SOURCE_PROJECT/config" \
    --include="*.json" --include="*.toml" \
    2>/dev/null | grep -iE "138" | head -1 || echo "")

echo "Chain ID 138 references:"
if [[ -n "$CHAIN_ID_PROXMOX" ]]; then
    echo "  ✅ Proxmox project: Found Chain ID 138"
else
    echo "  ⚠️  Proxmox project: Chain ID 138 not explicitly found"
fi

if [[ -n "$CHAIN_ID_SOURCE" ]]; then
    echo "  ✅ Source project: Found Chain ID 138"
else
    echo "  ⚠️  Source project: Chain ID 138 not explicitly found"
fi

echo ""
echo "=== 6. Checking Documentation Consistency ==="
echo ""

# Check for README files
echo "Checking README files:"
for readme in "$SOURCE_PROJECT/README.md" "$PROXMOX_PROJECT/README.md"; do
    if [[ -f "$readme" ]]; then
        echo "  ✅ Found: $(basename $(dirname "$readme"))/README.md"
    else
        echo "  ⚠️  Not found: $(basename $(dirname "$readme"))/README.md"
    fi
done

echo ""
echo "=== 7. Checking Service Configuration ==="
echo ""

# Check for service directories
SERVICES_DIR="$SOURCE_PROJECT/services"
if [[ -d "$SERVICES_DIR" ]]; then
    SERVICE_COUNT=$(find "$SERVICES_DIR" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | wc -l)
    echo "  ✅ Services directory exists: $SERVICES_DIR"
    echo "  Found $SERVICE_COUNT service directories"
    
    # List services
    echo "  Services:"
    find "$SERVICES_DIR" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | while read dir; do
        echo "    - $(basename "$dir")"
    done
else
    echo "  ⚠️  Services directory not found: $SERVICES_DIR"
fi

echo ""
echo "=== Summary ==="
echo ""
echo "Review complete. Check warnings above for potential inconsistencies."
echo ""

