#!/bin/bash

# Phase 10: Verification
# This script verifies the deployment

set -e

echo "=== Phase 10: Verification ==="

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

echo ""
echo "--- Running Verification Script ---"

if [ -f scripts/verify-deployment.sh ]; then
    ./scripts/verify-deployment.sh
else
    echo "⚠️  verify-deployment.sh not found"
fi

echo ""
echo "--- Contract Verification Summary ---"

if command -v cast &> /dev/null && [ ! -z "$ETHEREUM_MAINNET_RPC" ]; then
    CONTRACTS=(
        "BOND_MANAGER"
        "CHALLENGE_MANAGER"
        "LIQUIDITY_POOL"
        "INBOX_ETH"
        "BRIDGE_SWAP_COORDINATOR"
        "ENHANCED_SWAP_ROUTER"
        "STABLECOIN_PEG_MANAGER"
        "COMMODITY_PEG_MANAGER"
        "ISO_CURRENCY_MANAGER"
        "BRIDGE_RESERVE_COORDINATOR"
    )
    
    echo "Checking contract codes..."
    for var in "${CONTRACTS[@]}"; do
        if [ ! -z "${!var}" ] && [ "${!var}" != "0x..." ]; then
            CODE_SIZE=$(cast code "${!var}" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null | wc -c || echo "0")
            if [ "$CODE_SIZE" -gt 2 ]; then
                echo "✓ ${var}: Contract has code"
            else
                echo "✗ ${var}: Contract has no code"
            fi
        else
            echo "⚠️  ${var}: Not set"
        fi
    done
else
    echo "⚠️  cast command not found or RPC not configured"
fi

echo ""
echo "--- Service Verification ---"

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

echo ""
echo "--- End-to-End Test Instructions ---"
echo "To test the complete bridge flow:"
echo ""
echo "1. Deposit on ChainID 138:"
echo "   cast send \$LOCKBOX_138 \\"
echo "     \"depositNative(address,bytes32)\" \\"
echo "     <recipient> \\"
echo "     \$(cast keccak \"test\") \\"
echo "     --value 1ether \\"
echo "     --rpc-url \$RPC_URL_138 \\"
echo "     --private-key \$PRIVATE_KEY"
echo ""
echo "2. Submit claim on Ethereum (use relayer service or manual call)"
echo ""
echo "3. Wait for challenge window (30 minutes)"
echo ""
echo "4. Finalize claim"
echo ""
echo "5. Verify swap executed"

echo ""
echo "=== Phase 10 Complete ==="
echo "Verification complete"

