#!/usr/bin/env bash
# Verify TokenAdminRegistry Configuration
# Task 24: Create TokenAdminRegistry Verification Script
# Usage: ./verify-token-admin-registry.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}"
WETH9_ADDRESS="$(resolve_address_value WETH9_ADDRESS WETH9_ADDRESS 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2)"
WETH10_ADDRESS="$(resolve_address_value WETH10_ADDRESS WETH10_ADDRESS 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f)"
CCIP_ROUTER="$(resolve_address_value CCIP_ROUTER_ADDRESS CCIP_ROUTER_ADDRESS 0x42DAb7b888Dd382bD5Adcf9E038dBF1fD03b4817)"

log_info "========================================="
log_info "TokenAdminRegistry Verification"
log_info "========================================="
log_info ""
log_info "RPC URL: $RPC_URL"
log_info ""

# Try to find TokenAdminRegistry address
# It might be part of Router or a separate contract
log_info "Step 1: Attempting to locate TokenAdminRegistry..."
log_info ""

# Method 1: Try to get from Router (if function exists)
log_info "  Method 1: Query Router for TokenAdminRegistry..."
TOKEN_ADMIN_REGISTRY=$(cast call "$CCIP_ROUTER" "getTokenAdminRegistry()" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$TOKEN_ADMIN_REGISTRY" ] && [ "$TOKEN_ADMIN_REGISTRY" != "0x" ]; then
    TOKEN_ADMIN_CLEAN=$(echo "$TOKEN_ADMIN_REGISTRY" | grep -oE "^0x[0-9a-fA-F]{40}$" | head -1 || echo "")
    if [ -n "$TOKEN_ADMIN_CLEAN" ]; then
        log_success "  TokenAdminRegistry found: $TOKEN_ADMIN_CLEAN"
        TOKEN_ADMIN_REGISTRY="$TOKEN_ADMIN_CLEAN"
    else
        log_warn "  getTokenAdminRegistry() returned invalid address"
        TOKEN_ADMIN_REGISTRY=""
    fi
else
    log_warn "  getTokenAdminRegistry() function not available on Router"
    TOKEN_ADMIN_REGISTRY=""
fi

# If not found, try alternative methods
if [ -z "$TOKEN_ADMIN_REGISTRY" ]; then
    log_info "  Method 2: TokenAdminRegistry may be embedded in Router or separate contract"
    log_warn "  Cannot automatically locate TokenAdminRegistry address"
    log_info "  Note: TokenAdminRegistry address may need to be retrieved from deployment records"
    log_info ""
    log_info "  To manually verify:"
    log_info "    1. Check deployment transactions"
    log_info "    2. Check contract storage"
    log_info "    3. Check Chainlink documentation"
    log_info ""
    exit 0
fi

# Step 2: Verify WETH9 token registration
log_info ""
log_info "Step 2: Verifying WETH9 token registration..."
log_info "  Token Address: $WETH9_ADDRESS"

# Try to get pool address for WETH9
POOL_WETH9=$(cast call "$TOKEN_ADMIN_REGISTRY" "getPool(address)" "$WETH9_ADDRESS" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$POOL_WETH9" ] && [ "$POOL_WETH9" != "0x" ]; then
    POOL_WETH9_CLEAN=$(echo "$POOL_WETH9" | grep -oE "^0x[0-9a-fA-F]{40}$" | head -1 || echo "")
    if [ -n "$POOL_WETH9_CLEAN" ] && ! echo "$POOL_WETH9_CLEAN" | grep -qE "^0x0+$"; then
        log_success "  WETH9 Pool Address: $POOL_WETH9_CLEAN"
    else
        log_warn "  WETH9: Not registered or pool address is zero"
    fi
else
    log_warn "  WETH9: getPool() function not available or token not registered"
fi

# Step 3: Verify WETH10 token registration
log_info ""
log_info "Step 3: Verifying WETH10 token registration..."
log_info "  Token Address: $WETH10_ADDRESS"

# Try to get pool address for WETH10
POOL_WETH10=$(cast call "$TOKEN_ADMIN_REGISTRY" "getPool(address)" "$WETH10_ADDRESS" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$POOL_WETH10" ] && [ "$POOL_WETH10" != "0x" ]; then
    POOL_WETH10_CLEAN=$(echo "$POOL_WETH10" | grep -oE "^0x[0-9a-fA-F]{40}$" | head -1 || echo "")
    if [ -n "$POOL_WETH10_CLEAN" ] && ! echo "$POOL_WETH10_CLEAN" | grep -qE "^0x0+$"; then
        log_success "  WETH10 Pool Address: $POOL_WETH10_CLEAN"
    else
        log_warn "  WETH10: Not registered or pool address is zero"
    fi
else
    log_warn "  WETH10: getPool() function not available or token not registered"
fi

# Summary
log_info ""
log_info "========================================="
log_info "Verification Summary"
log_info "========================================="
if [ -n "$TOKEN_ADMIN_REGISTRY" ]; then
    log_success "✓ TokenAdminRegistry located: $TOKEN_ADMIN_REGISTRY"
else
    log_warn "⚠ TokenAdminRegistry address not found"
    log_info "  Manual verification required"
fi
log_info ""
