#!/usr/bin/env bash
# Configure CCIP Router with supported chains and tokens

set -e

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

# Load environment
if [ -f "$PROJECT_ROOT/.env" ]; then
    source "$PROJECT_ROOT/.env"
fi

RPC_URL="${RPC_URL_138:-${CHAIN138_RPC_URL:-http://localhost:8545}}"
PRIVATE_KEY="${PRIVATE_KEY:-}"

if [ -z "$PRIVATE_KEY" ]; then
    log_error "Error: PRIVATE_KEY environment variable not set"
    exit 1
fi

CCIP_ROUTER="${CCIP_CHAIN138_ROUTER:-${CCIP_ROUTER:-}}"

if [ -z "$CCIP_ROUTER" ]; then
    log_error "Error: CCIP_CHAIN138_ROUTER not set. Deploy router first."
    exit 1
fi

log_info "Configuring CCIP Router: $CCIP_ROUTER"
log_info "RPC URL: $RPC_URL"

# Chain selectors
ETH_MAINNET_SELECTOR="${ETH_MAINNET_SELECTOR:-5009297550715157269}"
CHAIN138_SELECTOR="${CHAIN138_SELECTOR:-138}"

# Token addresses (from genesis)
WETH9_ADDRESS="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
WETH10_ADDRESS="0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f"

log_info "Adding supported chains..."

# Add Ethereum Mainnet as supported chain
log_info "Adding Ethereum Mainnet (selector: $ETH_MAINNET_SELECTOR)..."
if cast send "$CCIP_ROUTER" "addSupportedChain(uint64)" "$ETH_MAINNET_SELECTOR" \
    --rpc-url "$RPC_URL" \
    --private-key "$PRIVATE_KEY" \
    --legacy 2>&1 | grep -q "Success\|success\|transactionHash"; then
    log_success "✅ Ethereum Mainnet added as supported chain"
else
    log_warn "⚠️  Failed to add Ethereum Mainnet (may already be added)"
fi

# Add supported tokens for Ethereum Mainnet
log_info "Adding supported tokens for Ethereum Mainnet..."

log_info "Adding WETH9 ($WETH9_ADDRESS)..."
if cast send "$CCIP_ROUTER" "addSupportedToken(uint64,address)" "$ETH_MAINNET_SELECTOR" "$WETH9_ADDRESS" \
    --rpc-url "$RPC_URL" \
    --private-key "$PRIVATE_KEY" \
    --legacy 2>&1 | grep -q "Success\|success\|transactionHash"; then
    log_success "✅ WETH9 added as supported token"
else
    log_warn "⚠️  Failed to add WETH9 (may already be added)"
fi

log_info "Adding WETH10 ($WETH10_ADDRESS)..."
if cast send "$CCIP_ROUTER" "addSupportedToken(uint64,address)" "$ETH_MAINNET_SELECTOR" "$WETH10_ADDRESS" \
    --rpc-url "$RPC_URL" \
    --private-key "$PRIVATE_KEY" \
    --legacy 2>&1 | grep -q "Success\|success\|transactionHash"; then
    log_success "✅ WETH10 added as supported token"
else
    log_warn "⚠️  Failed to add WETH10 (may already be added)"
fi

# Verify configuration
log_info "Verifying configuration..."

ETH_CHAIN_SUPPORTED=$(cast call "$CCIP_ROUTER" "supportedChains(uint64)" "$ETH_MAINNET_SELECTOR" --rpc-url "$RPC_URL" 2>/dev/null || echo "false")
if [ "$ETH_CHAIN_SUPPORTED" = "true" ]; then
    log_success "✅ Ethereum Mainnet is supported"
else
    log_warn "⚠️  Ethereum Mainnet support verification failed"
fi

log_success "✅ CCIP Router configuration complete!"
echo ""
echo "Router Address: $CCIP_ROUTER"
echo "Supported Chains:"
echo "  - Ethereum Mainnet ($ETH_MAINNET_SELECTOR)"
echo "Supported Tokens (Ethereum Mainnet):"
echo "  - WETH9: $WETH9_ADDRESS"
echo "  - WETH10: $WETH10_ADDRESS"

