#!/bin/bash

# Verify Deployment
# This script verifies that all contracts and services are deployed correctly

set -e

echo "=== Verifying Deployment ==="

# Load environment variables
if [ -f .env ]; then
    export $(cat .env | grep -v '^#' | xargs)
fi

# Check required environment variables
REQUIRED_VARS=(
    "ETHEREUM_MAINNET_RPC"
    "BOND_MANAGER"
    "CHALLENGE_MANAGER"
    "LIQUIDITY_POOL"
    "INBOX_ETH"
    "BRIDGE_SWAP_COORDINATOR"
    "ENHANCED_SWAP_ROUTER"
)

for var in "${REQUIRED_VARS[@]}"; do
    if [ -z "${!var}" ]; then
        echo "Warning: $var is not set"
    else
        echo "✓ $var: ${!var}"
    fi
done

echo ""
echo "--- Verifying Contracts ---"

# Verify contract code exists
if command -v cast &> /dev/null; then
    echo ""
    echo "Checking contract codes..."
    
    for var in "${REQUIRED_VARS[@]}"; do
        if [ ! -z "${!var}" ]; then
            CODE_SIZE=$(cast code "${!var}" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null | wc -c)
            if [ "$CODE_SIZE" -gt 2 ]; then
                echo "✓ ${var}: Contract has code"
            else
                echo "✗ ${var}: Contract has no code"
            fi
        fi
    done
fi

echo ""
echo "--- Verifying Services ---"

# Check if services are running
if command -v docker &> /dev/null; then
    SERVICES=(
        "liquidity-engine-service"
        "market-reporting-service"
        "bridge-reserve-service"
        "iso-currency-service"
    )
    
    for service in "${SERVICES[@]}"; do
        if docker ps --format '{{.Names}}' | grep -q "^${service}$"; then
            echo "✓ $service: Running"
        else
            echo "✗ $service: Not running"
        fi
    done
fi

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

