#!/usr/bin/env bash
# Inspect WETH10 contract implementation
# Checks if the contract matches standard WETH behavior
# Usage: ./inspect-weth10-contract.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
source "$PROJECT_ROOT/scripts/lib/address-inventory.sh"

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

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

load_explorer_runtime_env

# Configuration
RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
WETH10_ADDRESS="$(resolve_address_value WETH10_ADDRESS WETH10_ADDRESS 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f)"

log_info "========================================="
log_info "WETH10 Contract Inspection"
log_info "========================================="
log_info ""
log_info "Contract Address: $WETH10_ADDRESS"
log_info "RPC URL: $RPC_URL"
log_info ""

# Step 1: Check contract exists and has code
log_info "Step 1: Checking contract existence..."
BYTECODE=$(cast code "$WETH10_ADDRESS" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -z "$BYTECODE" ] || [ "$BYTECODE" = "0x" ]; then
    log_error "Contract has no bytecode at address $WETH10_ADDRESS"
    exit 1
fi

BYTECODE_LENGTH=$(echo -n "$BYTECODE" | wc -c)
log_success "✓ Contract exists (bytecode length: $BYTECODE_LENGTH chars)"
log_info ""

# Step 2: Check standard ERC-20 functions
log_info "Step 2: Checking ERC-20 functions..."

# Check balanceOf
BALANCEOF_RESULT=$(cast call "$WETH10_ADDRESS" "balanceOf(address)" "0x0000000000000000000000000000000000000000" --rpc-url "$RPC_URL" 2>&1 || echo "ERROR")
if echo "$BALANCEOF_RESULT" | grep -qE "^(0x)?[0-9a-fA-F]+$"; then
    log_success "✓ balanceOf() function exists"
else
    log_warn "⚠ balanceOf() may not exist or has issues"
fi

# Check totalSupply
TOTALSUPPLY_RESULT=$(cast call "$WETH10_ADDRESS" "totalSupply()" --rpc-url "$RPC_URL" 2>&1 || echo "ERROR")
if echo "$TOTALSUPPLY_RESULT" | grep -qE "^(0x)?[0-9a-fA-F]+$"; then
    TOTALSUPPLY_ETH=$(echo "scale=18; $TOTALSUPPLY_RESULT / 1000000000000000000" | bc 2>/dev/null || echo "N/A")
    log_success "✓ totalSupply() function exists (Current: $TOTALSUPPLY_ETH WETH10)"
else
    log_warn "⚠ totalSupply() may not exist or has issues"
fi

# Check decimals
DECIMALS_RESULT=$(cast call "$WETH10_ADDRESS" "decimals()" --rpc-url "$RPC_URL" 2>&1 || echo "ERROR")
if echo "$DECIMALS_RESULT" | grep -qE "^(0x)?[0-9a-fA-F]+$"; then
    DECIMALS=$(cast --to-dec "$DECIMALS_RESULT" 2>/dev/null || echo "N/A")
    if [ "$DECIMALS" = "18" ]; then
        log_success "✓ decimals() returns 18 (correct)"
    else
        log_warn "⚠ decimals() returns $DECIMALS (expected 18)"
    fi
else
    log_warn "⚠ decimals() may not exist"
fi

log_info ""

# Step 3: Check contract ETH balance vs total supply
log_info "Step 3: Verifying 1:1 backing..."
CONTRACT_ETH=$(cast balance "$WETH10_ADDRESS" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
TOTAL_SUPPLY=$(cast call "$WETH10_ADDRESS" "totalSupply()" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")

CONTRACT_ETH_ETH=$(echo "scale=18; $CONTRACT_ETH / 1000000000000000000" | bc 2>/dev/null || echo "0")
TOTAL_SUPPLY_ETH=$(echo "scale=18; $TOTAL_SUPPLY / 1000000000000000000" | bc 2>/dev/null || echo "0")

log_info "Contract ETH Balance: $CONTRACT_ETH_ETH ETH ($CONTRACT_ETH wei)"
log_info "WETH10 Total Supply: $TOTAL_SUPPLY_ETH WETH10 ($TOTAL_SUPPLY wei)"

if [ "$CONTRACT_ETH" = "$TOTAL_SUPPLY" ]; then
    log_success "✓ Contract balance matches total supply (1:1 backing verified)"
else
    log_error "✗ Contract balance does NOT match total supply!"
    log_error "  This indicates a potential issue with the contract implementation"
    DIFF=$(echo "$CONTRACT_ETH - $TOTAL_SUPPLY" | bc 2>/dev/null || echo "0")
    log_error "  Difference: $DIFF wei"
fi

log_info ""

# Step 4: Summary and recommendations
log_info "========================================="
log_info "Summary"
log_info "========================================="
log_info ""

if [ "$CONTRACT_ETH" = "$TOTAL_SUPPLY" ]; then
    log_success "✓ Contract appears to maintain 1:1 backing"
else
    log_error "✗ Contract does NOT maintain 1:1 backing!"
fi

log_info ""
log_info "For detailed analysis, use:"
log_info "  cast code $WETH10_ADDRESS --rpc-url $RPC_URL > weth10_bytecode.bin"
