#!/usr/bin/env bash
# Verify Token Pool Configuration
# Task 32: Create Pool Configuration Verification Script
# Usage: ./verify-token-pool-config.sh [pool_address]

set -euo pipefail

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

# 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 environment variables if .env exists
if [ -f "$PROJECT_ROOT/.env" ]; then
    source "$PROJECT_ROOT/.env"
elif [ -f "$PROJECT_ROOT/../.env" ]; then
    source "$PROJECT_ROOT/../.env"
fi

# Configuration
RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
POOL_ADDRESS="${1:-}"

log_info "========================================="
log_info "Token Pool Configuration Verification"
log_info "========================================="
log_info ""

if [ -z "$POOL_ADDRESS" ]; then
    log_error "Pool address required"
    log_info "Usage: $0 <pool_address>"
    log_info ""
    log_info "To find pool addresses, first run:"
    log_info "  ./scripts/verify-token-admin-registry.sh"
    exit 1
fi

log_info "Pool Address: $POOL_ADDRESS"
log_info "RPC URL: $RPC_URL"
log_info ""

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

BYTECODE_LENGTH=$(echo -n "$BYTECODE" | wc -c)
log_success "Pool contract exists (bytecode length: $BYTECODE_LENGTH characters)"

# Step 2: Try to get remote chain configuration
log_info ""
log_info "Step 2: Checking remote chain configuration..."

# Try to get supported chains (if function exists)
log_info "  Testing getSupportedChains()..."
SUPPORTED_CHAINS=$(cast call "$POOL_ADDRESS" "getSupportedChains()" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$SUPPORTED_CHAINS" ] && [ "$SUPPORTED_CHAINS" != "0x" ]; then
    log_success "  getSupportedChains(): Available"
    log_info "    Result: $SUPPORTED_CHAINS"
else
    log_warn "  getSupportedChains(): Function not available"
fi

# Step 3: Try to get rate limits
log_info ""
log_info "Step 3: Checking rate limits..."

# Try to get outbound rate limit
log_info "  Testing getOutboundRateLimit()..."
OUTBOUND_LIMIT=$(cast call "$POOL_ADDRESS" "getOutboundRateLimit()" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$OUTBOUND_LIMIT" ] && [ "$OUTBOUND_LIMIT" != "0x" ]; then
    log_success "  Outbound Rate Limit: $OUTBOUND_LIMIT"
else
    log_warn "  getOutboundRateLimit(): Function not available"
fi

# Try to get inbound rate limit
log_info "  Testing getInboundRateLimit()..."
INBOUND_LIMIT=$(cast call "$POOL_ADDRESS" "getInboundRateLimit()" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$INBOUND_LIMIT" ] && [ "$INBOUND_LIMIT" != "0x" ]; then
    log_success "  Inbound Rate Limit: $INBOUND_LIMIT"
else
    log_warn "  getInboundRateLimit(): Function not available"
fi

# Step 4: Try to get pool permissions
log_info ""
log_info "Step 4: Checking pool permissions..."

# Try to check if pool can mint
log_info "  Testing canMint()..."
CAN_MINT=$(cast call "$POOL_ADDRESS" "canMint()" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$CAN_MINT" ] && [ "$CAN_MINT" != "0x" ]; then
    if echo "$CAN_MINT" | grep -qi "true\|1"; then
        log_success "  Mint Permission: Enabled"
    else
        log_warn "  Mint Permission: Disabled"
    fi
else
    log_warn "  canMint(): Function not available"
fi

# Summary
log_info ""
log_info "========================================="
log_info "Verification Summary"
log_info "========================================="
log_success "✓ Pool contract exists and is accessible"
log_info "  Address: $POOL_ADDRESS"
log_info "  Bytecode: Present ($BYTECODE_LENGTH characters)"
log_warn "  Note: Some functions may not be available or accessible"
log_info "  Manual verification may be required for full configuration"
log_info ""

