#!/bin/bash
# Test script for MainnetTether and TransactionMirror contracts

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../" && pwd)"

cd "$PROJECT_ROOT"

# Source environment variables
if [ -f .env ]; then
    source .env
else
    echo "❌ Error: .env file not found"
    exit 1
fi

# Contract addresses
MAINNET_TETHER="${MAINNET_TETHER_ADDRESS:-0x15DF1D5BFDD8Aa4b380445D4e3E9B38d34283619}"
TRANSACTION_MIRROR="${TRANSACTION_MIRROR_ADDRESS:-0x4CF42c4F1dBa748601b8938be3E7ABD732E87cE9}"
DEPLOYER="0x4A666F96fC8764181194447A7dFdb7d471b301C8"

echo "=== Testing Deployed Contracts ==="
echo ""
echo "MainnetTether: $MAINNET_TETHER"
echo "TransactionMirror: $TRANSACTION_MIRROR"
echo "Deployer: $DEPLOYER"
echo ""

# Test MainnetTether
echo "=== Testing MainnetTether ==="
echo ""

echo "1. Checking admin..."
TETHER_ADMIN=$(cast call $MAINNET_TETHER "admin()" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null)
echo "   Admin: $TETHER_ADMIN"
if [ "$(echo $TETHER_ADMIN | tr '[:upper:]' '[:lower:]')" = "$(echo $DEPLOYER | tr '[:upper:]' '[:lower:]')" ]; then
    echo "   ✅ Admin matches deployer"
else
    echo "   ⚠️  Admin does not match deployer"
fi

echo ""
echo "2. Checking paused status..."
PAUSED=$(cast call $MAINNET_TETHER "paused()" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null)
if [ "$PAUSED" = "0x0000000000000000000000000000000000000000000000000000000000000000" ]; then
    echo "   ✅ Contract is not paused"
else
    echo "   ⚠️  Contract is paused"
fi

echo ""
echo "3. Checking CHAIN_138 constant..."
CHAIN_138=$(cast call $MAINNET_TETHER "CHAIN_138()" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null)
echo "   CHAIN_138: $CHAIN_138"

echo ""
echo "4. Testing isAnchored (block 0)..."
IS_ANCHORED=$(cast call $MAINNET_TETHER "isAnchored(uint256)" 0 --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null)
if [ "$IS_ANCHORED" = "0x0000000000000000000000000000000000000000000000000000000000000000" ]; then
    echo "   ✅ Block 0 is not anchored (expected)"
else
    echo "   ⚠️  Block 0 is anchored"
fi

# Test TransactionMirror
echo ""
echo "=== Testing TransactionMirror ==="
echo ""

echo "1. Checking admin..."
MIRROR_ADMIN=$(cast call $TRANSACTION_MIRROR "admin()" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null)
echo "   Admin: $MIRROR_ADMIN"
if [ "$(echo $MIRROR_ADMIN | tr '[:upper:]' '[:lower:]')" = "$(echo $DEPLOYER | tr '[:upper:]' '[:lower:]')" ]; then
    echo "   ✅ Admin matches deployer"
else
    echo "   ⚠️  Admin does not match deployer"
fi

echo ""
echo "2. Checking paused status..."
PAUSED=$(cast call $TRANSACTION_MIRROR "paused()" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null)
if [ "$PAUSED" = "0x0000000000000000000000000000000000000000000000000000000000000000" ]; then
    echo "   ✅ Contract is not paused"
else
    echo "   ⚠️  Contract is paused"
fi

echo ""
echo "3. Checking CHAIN_138 constant..."
CHAIN_138=$(cast call $TRANSACTION_MIRROR "CHAIN_138()" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null)
echo "   CHAIN_138: $CHAIN_138"

echo ""
echo "4. Checking MAX_BATCH_SIZE..."
MAX_BATCH=$(cast call $TRANSACTION_MIRROR "MAX_BATCH_SIZE()" --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null)
echo "   MAX_BATCH_SIZE: $(cast --to-dec $MAX_BATCH 2>/dev/null || echo $MAX_BATCH)"

echo ""
echo "5. Testing isMirrored (zero hash)..."
ZERO_HASH="0x0000000000000000000000000000000000000000000000000000000000000000"
IS_MIRRORED=$(cast call $TRANSACTION_MIRROR "isMirrored(bytes32)" $ZERO_HASH --rpc-url "$ETHEREUM_MAINNET_RPC" 2>/dev/null)
if [ "$IS_MIRRORED" = "0x0000000000000000000000000000000000000000000000000000000000000000" ]; then
    echo "   ✅ Zero hash is not mirrored (expected)"
else
    echo "   ⚠️  Zero hash is mirrored"
fi

echo ""
echo "=== Test Summary ==="
echo "✅ All basic contract tests completed"
echo ""
echo "Next Steps:"
echo "  1. Test pause/unpause functionality (requires admin)"
echo "  2. Test state proof anchoring (requires Chain-138 data)"
echo "  3. Test transaction mirroring (requires Chain-138 transactions)"
echo "  4. Monitor contract events on Etherscan"

