#!/usr/bin/env bash
# Comprehensive testing suite for bridge system
# Usage: ./test-suite.sh [test_name]

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_PROJECT="/home/intlc/projects/smom-dbis-138"

source "$SOURCE_PROJECT/.env" 2>/dev/null || true

RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
TEST_AMOUNT="0.001"  # Small test amount

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

PASSED=0
FAILED=0

test_pass() {
    echo -e "${GREEN}[PASS]${NC} $1"
    ((PASSED++))
}

test_fail() {
    echo -e "${RED}[FAIL]${NC} $1"
    ((FAILED++))
}

test_info() {
    echo -e "${BLUE}[TEST]${NC} $1"
}

# Test RPC connectivity
test_rpc_connectivity() {
    test_info "Testing RPC connectivity..."
    if cast block-number --rpc-url "$RPC_URL" >/dev/null 2>&1; then
        test_pass "RPC is accessible"
    else
        test_fail "RPC is not accessible"
    fi
}

# Test bridge contracts
test_bridge_contracts() {
    test_info "Testing bridge contracts..."
    
    WETH9_BRIDGE="${CCIPWETH9_BRIDGE_CHAIN138:-0x89dd12025bfCD38A168455A44B400e913ED33BE2}"
    WETH10_BRIDGE="${CCIPWETH10_BRIDGE_CHAIN138:-0xe0E93247376aa097dB308B92e6Ba36bA015535D0}"
    
    if cast code "$WETH9_BRIDGE" --rpc-url "$RPC_URL" >/dev/null 2>&1; then
        test_pass "WETH9 Bridge deployed"
    else
        test_fail "WETH9 Bridge not found"
    fi
    
    if cast code "$WETH10_BRIDGE" --rpc-url "$RPC_URL" >/dev/null 2>&1; then
        test_pass "WETH10 Bridge deployed"
    else
        test_fail "WETH10 Bridge not found"
    fi
}

# Test destination chains
test_destinations() {
    test_info "Testing destination chain configuration..."
    
    WETH9_BRIDGE="${CCIPWETH9_BRIDGE_CHAIN138:-0x89dd12025bfCD38A168455A44B400e913ED33BE2}"
    
    declare -A CHAINS=(
        ["BSC"]="11344663589394136015"
        ["Polygon"]="4051577828743386545"
        ["Avalanche"]="6433500567565415381"
        ["Base"]="15971525489660198786"
        ["Arbitrum"]="4949039107694359620"
        ["Optimism"]="3734403246176062136"
        ["Ethereum"]="5009297550715157269"
    )
    
    local all_configured=true
    for chain in "${!CHAINS[@]}"; do
        selector="${CHAINS[$chain]}"
        result=$(cast call "$WETH9_BRIDGE" "destinations(uint64)" "$selector" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
        if [ -n "$result" ] && ! echo "$result" | grep -q "0x0000000000000000000000000000000000000000$"; then
            test_pass "$chain configured"
        else
            test_fail "$chain not configured"
            all_configured=false
        fi
    done
    
    if [ "$all_configured" = true ]; then
        test_pass "All destination chains configured"
    fi
}

# Test fee calculation
test_fee_calculation() {
    test_info "Testing fee calculation..."
    
    WETH9_BRIDGE="${CCIPWETH9_BRIDGE_CHAIN138:-0x89dd12025bfCD38A168455A44B400e913ED33BE2}"
    AMOUNT_WEI=$(cast --to-wei "$TEST_AMOUNT" ether 2>/dev/null)
    BSC_SELECTOR="11344663589394136015"
    
    FEE=$(cast call "$WETH9_BRIDGE" "calculateFee(uint64,uint256)" "$BSC_SELECTOR" "$AMOUNT_WEI" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
    
    if [ "$FEE" != "0" ] && [ -n "$FEE" ]; then
        test_pass "Fee calculation working"
    else
        test_fail "Fee calculation failed"
    fi
}

# Test balances
test_balances() {
    test_info "Testing balances..."
    
    DEPLOYER=$(cast wallet address --private-key "$PRIVATE_KEY" 2>/dev/null || echo "")
    if [ -z "$DEPLOYER" ]; then
        test_fail "Cannot determine deployer address"
        return
    fi
    
    ETH_BAL=$(cast balance "$DEPLOYER" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
    ETH_BAL_ETH=$(echo "scale=4; $ETH_BAL / 1000000000000000000" | bc 2>/dev/null || echo "0")
    
    if (( $(echo "$ETH_BAL_ETH > 0.1" | bc -l 2>/dev/null || echo 0) )); then
        test_pass "Sufficient ETH balance: $ETH_BAL_ETH ETH"
    else
        test_fail "Low ETH balance: $ETH_BAL_ETH ETH"
    fi
}

# Test gas price fetching
test_gas_price() {
    test_info "Testing gas price fetching..."
    
    GAS_PRICE=$(cast gas-price --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    if [ -n "$GAS_PRICE" ] && [ "$GAS_PRICE" != "0" ]; then
        test_pass "Gas price fetched: $(echo "scale=2; $GAS_PRICE / 1000000000" | bc) gwei"
    else
        test_fail "Gas price fetch failed"
    fi
}

# Run all tests
run_all_tests() {
    echo "=== Bridge System Test Suite ==="
    echo ""
    
    test_rpc_connectivity
    test_bridge_contracts
    test_destinations
    test_fee_calculation
    test_balances
    test_gas_price
    
    echo ""
    echo "=== Test Results ==="
    echo "Passed: $PASSED"
    echo "Failed: $FAILED"
    echo "Total: $((PASSED + FAILED))"
    
    if [ $FAILED -eq 0 ]; then
        echo -e "${GREEN}All tests passed!${NC}"
        return 0
    else
        echo -e "${RED}Some tests failed!${NC}"
        return 1
    fi
}

# Main execution
TEST_NAME="${1:-all}"

case "$TEST_NAME" in
    rpc) test_rpc_connectivity ;;
    contracts) test_bridge_contracts ;;
    destinations) test_destinations ;;
    fees) test_fee_calculation ;;
    balances) test_balances ;;
    gas) test_gas_price ;;
    all) run_all_tests ;;
    *)
        echo "Unknown test: $TEST_NAME"
        echo "Available tests: rpc, contracts, destinations, fees, balances, gas, all"
        exit 1
        ;;
esac

