#!/usr/bin/env bash
# Get correct token information for WETH9 and WETH10
# Provides correct decimals (18) even if contract returns 0
# Usage: ./get-token-info.sh [weth9|weth10|both]

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"; }

load_explorer_runtime_env

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

# Correct decimals (contract may return wrong value)
WETH9_DECIMALS=18
WETH10_DECIMALS=18

TOKEN="${1:-both}"

log_info "========================================="
log_info "Token Information (Corrected)"
log_info "========================================="
log_info ""

if [ "$TOKEN" = "weth9" ] || [ "$TOKEN" = "both" ]; then
    log_info "WETH9 Token Information:"
    log_info "  Address: $WETH9_ADDRESS"
    log_info "  Symbol: WETH"
    log_info "  Name: Wrapped Ether"
    log_info "  Decimals: $WETH9_DECIMALS (corrected - contract returns 0)"
    
    # Get on-chain data
    TOTAL_SUPPLY=$(cast call "$WETH9_ADDRESS" "totalSupply()" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
    CONTRACT_BALANCE=$(cast balance "$WETH9_ADDRESS" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
    
    # Convert hex to decimal if needed
    if echo "$TOTAL_SUPPLY" | grep -q "^0x"; then
        TOTAL_SUPPLY_DEC=$(cast --to-dec "$TOTAL_SUPPLY" 2>/dev/null || echo "0")
    else
        TOTAL_SUPPLY_DEC="$TOTAL_SUPPLY"
    fi
    
    TOTAL_SUPPLY_ETH=$(echo "scale=18; $TOTAL_SUPPLY_DEC / 1000000000000000000" | bc 2>/dev/null || echo "0")
    CONTRACT_BALANCE_ETH=$(echo "scale=18; $CONTRACT_BALANCE / 1000000000000000000" | bc 2>/dev/null || echo "0")
    
    log_info "  Total Supply: $TOTAL_SUPPLY_ETH WETH"
    log_info "  Contract Balance: $CONTRACT_BALANCE_ETH ETH"
    
    # Compare in wei (both should be same value)
    if [ "$CONTRACT_BALANCE" = "$TOTAL_SUPPLY_DEC" ]; then
        log_success "  1:1 Backing: ✅ Yes"
    else
        log_warn "  1:1 Backing: ❌ No (Contract: $CONTRACT_BALANCE, Supply: $TOTAL_SUPPLY_DEC)"
    fi
    log_info ""
fi

if [ "$TOKEN" = "weth10" ] || [ "$TOKEN" = "both" ]; then
    log_info "WETH10 Token Information:"
    log_info "  Address: $WETH10_ADDRESS"
    log_info "  Symbol: WETH10"
    log_info "  Name: Wrapped Ether 10"
    log_info "  Decimals: $WETH10_DECIMALS (correct)"
    
    # Get on-chain data
    TOTAL_SUPPLY=$(cast call "$WETH10_ADDRESS" "totalSupply()" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
    CONTRACT_BALANCE=$(cast balance "$WETH10_ADDRESS" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
    
    # Convert hex to decimal if needed
    if echo "$TOTAL_SUPPLY" | grep -q "^0x"; then
        TOTAL_SUPPLY_DEC=$(cast --to-dec "$TOTAL_SUPPLY" 2>/dev/null || echo "0")
    else
        TOTAL_SUPPLY_DEC="$TOTAL_SUPPLY"
    fi
    
    TOTAL_SUPPLY_ETH=$(echo "scale=18; $TOTAL_SUPPLY_DEC / 1000000000000000000" | bc 2>/dev/null || echo "0")
    CONTRACT_BALANCE_ETH=$(echo "scale=18; $CONTRACT_BALANCE / 1000000000000000000" | bc 2>/dev/null || echo "0")
    
    log_info "  Total Supply: $TOTAL_SUPPLY_ETH WETH10"
    log_info "  Contract Balance: $CONTRACT_BALANCE_ETH ETH"
    
    # Compare in wei
    if [ "$CONTRACT_BALANCE" = "$TOTAL_SUPPLY_DEC" ]; then
        log_success "  1:1 Backing: ✅ Yes"
    elif [ "$TOTAL_SUPPLY_DEC" = "0" ] && [ "$CONTRACT_BALANCE" = "0" ]; then
        log_info "  1:1 Backing: ℹ️ No tokens minted yet"
    else
        log_warn "  1:1 Backing: ❌ No (Contract: $CONTRACT_BALANCE, Supply: $TOTAL_SUPPLY_DEC)"
    fi
    log_info ""
fi

log_info "========================================="
log_info "Usage in Code"
log_info "========================================="
log_info ""
log_info "JavaScript/TypeScript (ethers.js):"
log_info "  const decimals = 18; // Always use 18, don't read from contract"
log_info "  const balance = await contract.balanceOf(address);"
log_info "  const formatted = ethers.utils.formatUnits(balance, 18);"
log_info ""
log_info "Python (web3.py):"
log_info "  decimals = 18  # Always use 18"
log_info "  balance = contract.functions.balanceOf(address).call()"
log_info "  formatted = Web3.fromWei(balance, 'ether')"
log_info ""
