#!/usr/bin/env bash
# Validate Production Configuration

set -euo pipefail

source .env.production 2>/dev/null || {
    echo "Error: .env.production not found"
    exit 1
}

echo "Validating Production Configuration..."
echo ""

ERRORS=0

# Check required variables
REQUIRED_VARS=(
    "CHAIN138_RPC"
    "ETHEREUM_MAINNET_RPC"
    "LOCKBOX138_ADDRESS"
    "INBOX_ETH_ADDRESS"
    "BOND_MANAGER_ADDRESS"
    "CHALLENGE_MANAGER_ADDRESS"
    "LIQUIDITY_POOL_ADDRESS"
    "MULTISIG_ADDRESS"
)

for var in "${REQUIRED_VARS[@]}"; do
    if [ -z "${!var:-}" ]; then
        echo "❌ Missing: $var"
        ERRORS=$((ERRORS + 1))
    else
        echo "✅ $var is set"
    fi
done

# Validate addresses (not zero)
if [ "$LOCKBOX138_ADDRESS" = "0x0000000000000000000000000000000000000000" ]; then
    echo "❌ LOCKBOX138_ADDRESS is not set"
    ERRORS=$((ERRORS + 1))
fi

if [ "$MULTISIG_ADDRESS" = "0x0000000000000000000000000000000000000000" ]; then
    echo "❌ MULTISIG_ADDRESS is not set"
    ERRORS=$((ERRORS + 1))
fi

# Validate RPC connectivity
echo ""
echo "Testing RPC connectivity..."

if cast block-number --rpc-url "$CHAIN138_RPC" >/dev/null 2>&1; then
    echo "✅ ChainID 138 RPC is accessible"
else
    echo "❌ ChainID 138 RPC is not accessible"
    ERRORS=$((ERRORS + 1))
fi

if cast block-number --rpc-url "$ETHEREUM_MAINNET_RPC" >/dev/null 2>&1; then
    echo "✅ Ethereum Mainnet RPC is accessible"
else
    echo "❌ Ethereum Mainnet RPC is not accessible"
    ERRORS=$((ERRORS + 1))
fi

echo ""
if [ $ERRORS -eq 0 ]; then
    echo "✅ Production configuration is valid"
    exit 0
else
    echo "❌ Production configuration has $ERRORS error(s)"
    exit 1
fi
