#!/usr/bin/env bash
# Verify All RPC Endpoints — tests connectivity to Mainnet, Chain 138, and all configured chain RPCs.

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$PROJECT_ROOT"
# Load .env via dotenv (RPC CR/LF trim). Fallback: raw source.
if [[ -f "$SCRIPT_DIR/../lib/deployment/dotenv.sh" ]]; then
  # shellcheck disable=SC1090
  source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "${PROJECT_ROOT:-$REPO_ROOT}"
elif [[ -n "${PROJECT_ROOT:-}" && -f "$PROJECT_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$PROJECT_ROOT/.env"
  set +a
elif [[ -n "${REPO_ROOT:-}" && -f "$REPO_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$REPO_ROOT/.env"
  set +a
fi

echo "=== RPC Endpoint Verification ==="
echo ""

if [ -f .env ]; then
  set -a
  source .env
  set +a
fi

# Use Infura with Basic Auth when INFURA_PROJECT_SECRET is set (fixes "error sending request" from Infura)
[ -f "${PROJECT_ROOT}/scripts/lib/infura.sh" ] && source "${PROJECT_ROOT}/scripts/lib/infura.sh"

if ! command -v cast &> /dev/null; then
    echo "Error: cast command not found. Install foundry to verify RPC endpoints."
    exit 1
fi

# Function to test RPC (uses Infura Basic Auth URL when INFURA_PROJECT_SECRET is set)
test_rpc() {
    local name=$1
    local url=$2
    [ -n "$url" ] && type ensure_infura_rpc_url &>/dev/null && url=$(ensure_infura_rpc_url "$url")
    
    if [ -z "$url" ] || [ "$url" == "http://chain138.example.com:8545" ] || [[ "$url" == *"YOUR_PROJECT_ID"* ]] || [[ "$url" == *"<"* ]]; then
        echo "○ $name: Not configured"
        return
    fi
    
    echo -n "Testing $name... "
    BLOCK=$(cast block-number --rpc-url "$url" 2>&1) || true
    if [[ "$BLOCK" =~ ^[0-9]+$ ]]; then
        echo "✓ Connected (block: $BLOCK)"
    else
        echo "✗ Failed (${BLOCK:0:80})"
    fi
    return 0
}

echo "--- Required RPC Endpoints ---"
test_rpc "Ethereum Mainnet" "$ETHEREUM_MAINNET_RPC"
test_rpc "ChainID 138" "$RPC_URL_138"

echo ""
echo "--- Additional Network RPC Endpoints (Infura when set) ---"
test_rpc "Ethereum Sepolia" "$ETHEREUM_SEPOLIA_RPC"
test_rpc "Polygon Mainnet" "$POLYGON_MAINNET_RPC"
test_rpc "Polygon Amoy" "$POLYGON_AMOY_RPC"
test_rpc "Base Mainnet" "$BASE_MAINNET_RPC"
test_rpc "Base Sepolia" "$BASE_SEPOLIA_RPC"
test_rpc "Optimism Mainnet" "$OPTIMISM_MAINNET_RPC"
test_rpc "Optimism Sepolia" "$OPTIMISM_SEPOLIA_RPC"
test_rpc "Arbitrum Mainnet" "$ARBITRUM_MAINNET_RPC"
test_rpc "Arbitrum Sepolia" "$ARBITRUM_SEPOLIA_RPC"
test_rpc "Avalanche Mainnet" "$AVALANCHE_MAINNET_RPC"
test_rpc "Avalanche Fuji" "$AVALANCHE_FUJI_RPC"
test_rpc "BSC Mainnet" "$BSC_MAINNET_RPC"
test_rpc "Celo Mainnet" "$CELO_MAINNET_RPC"
test_rpc "Linea Mainnet" "$LINEA_MAINNET_RPC"

echo ""
echo "--- Other chain RPCs (aliases / public fallbacks) ---"
test_rpc "AVALANCHE_RPC_URL" "${AVALANCHE_RPC_URL:-}"
test_rpc "ARBITRUM_RPC" "${ARBITRUM_MAINNET_RPC:-$ARBITRUM_RPC}"
test_rpc "Cronos" "$CRONOS_RPC_URL"

echo ""
echo "=== Verification Complete ==="

