#!/bin/bash
# Tutus Testnet Verification Script
# Verifies both Alpha and Beta instances are running correctly

set -e

echo "=============================================="
echo "  TUTUS TESTNET VERIFICATION"
echo "=============================================="
echo ""

# Check Alpha instance
echo "=== Alpha Instance ==="
ALPHA_VERSION=$(curl -s http://localhost:10332 -d '{"jsonrpc":"2.0","method":"getversion","id":1}')
ALPHA_NETWORK=$(echo "$ALPHA_VERSION" | grep -oP '"network":\K\d+')
ALPHA_VALIDATORS=$(echo "$ALPHA_VERSION" | grep -oP '"validatorscount":\K\d+')
ALPHA_BLOCKTIME=$(echo "$ALPHA_VERSION" | grep -oP '"msperblock":\K\d+')

echo "  Network Magic: $ALPHA_NETWORK"
echo "  Validators: $ALPHA_VALIDATORS"
echo "  Block Time: ${ALPHA_BLOCKTIME}ms"
echo ""
echo "  Node Heights:"
for port in 10332 10342 10352; do
  height=$(curl -s http://localhost:$port -d '{"jsonrpc":"2.0","method":"getblockcount","id":1}' | grep -oP '"result":\K\d+')
  echo "    localhost:$port -> Block $height"
done

echo ""

# Check Beta instance
echo "=== Beta Instance ==="
BETA_VERSION=$(curl -s http://localhost:11332 -d '{"jsonrpc":"2.0","method":"getversion","id":1}')
BETA_NETWORK=$(echo "$BETA_VERSION" | grep -oP '"network":\K\d+')
BETA_VALIDATORS=$(echo "$BETA_VERSION" | grep -oP '"validatorscount":\K\d+')
BETA_BLOCKTIME=$(echo "$BETA_VERSION" | grep -oP '"msperblock":\K\d+')

echo "  Network Magic: $BETA_NETWORK"
echo "  Validators: $BETA_VALIDATORS"
echo "  Block Time: ${BETA_BLOCKTIME}ms"
echo ""
echo "  Node Heights:"
for port in 11332 11342 11352; do
  height=$(curl -s http://localhost:$port -d '{"jsonrpc":"2.0","method":"getblockcount","id":1}' | grep -oP '"result":\K\d+')
  echo "    localhost:$port -> Block $height"
done

echo ""
echo "=== Container Status ==="
docker ps --format "table {{.Names}}\t{{.Status}}" | grep -E "NAME|tutus"

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

# Validate expectations
if [ "$ALPHA_NETWORK" != "88890" ]; then
  echo "ERROR: Alpha network magic should be 88890, got $ALPHA_NETWORK"
  exit 1
fi

if [ "$BETA_NETWORK" != "88891" ]; then
  echo "ERROR: Beta network magic should be 88891, got $BETA_NETWORK"
  exit 1
fi

if [ "$ALPHA_VALIDATORS" != "3" ] || [ "$BETA_VALIDATORS" != "3" ]; then
  echo "ERROR: Both instances should have 3 validators"
  exit 1
fi

echo ""
echo "=== Native Contract Tests ==="

# Test Tutus Token
TUTUS_RESULT=$(curl -s http://localhost:10332 -d '{"jsonrpc":"2.0","method":"invokefunction","params":["0x2d2dcf3c8b6b96793f6ecfd5856bb39d536f1d89","totalSupply"],"id":1}')
TUTUS_STATE=$(echo "$TUTUS_RESULT" | grep -oP '"state":"\K[^"]+')
echo "  Tutus Token: $TUTUS_STATE"

# Test Lub Token
LUB_RESULT=$(curl -s http://localhost:10332 -d '{"jsonrpc":"2.0","method":"invokefunction","params":["0xe895bb9370f057a3d47964dd6f14aa5e8615e869","totalSupply"],"id":1}')
LUB_STATE=$(echo "$LUB_RESULT" | grep -oP '"state":"\K[^"]+')
echo "  Lub Token: $LUB_STATE"

# Test Vita
VITA_RESULT=$(curl -s http://localhost:10332 -d '{"jsonrpc":"2.0","method":"invokefunction","params":["0xde437f043fdfb8f9c8241acb82d0791dd4b3217d","totalSupply"],"id":1}')
VITA_STATE=$(echo "$VITA_RESULT" | grep -oP '"state":"\K[^"]+')
echo "  Vita: $VITA_STATE"

# Test Lex
LEX_RESULT=$(curl -s http://localhost:10332 -d '{"jsonrpc":"2.0","method":"invokefunction","params":["0xe696922df6c6ded4c468bec2b1ef170805b73f2e","getLawCount"],"id":1}')
LEX_STATE=$(echo "$LEX_RESULT" | grep -oP '"state":"\K[^"]+')
echo "  Lex: $LEX_STATE"

if [ "$TUTUS_STATE" != "HALT" ] || [ "$LUB_STATE" != "HALT" ] || [ "$VITA_STATE" != "HALT" ] || [ "$LEX_STATE" != "HALT" ]; then
  echo "ERROR: Native contract calls failed"
  exit 1
fi

echo ""
echo "All checks passed!"
