#!/bin/bash

# Configure Bridge Destinations Script
# Configures ChainID 138 ↔ Mainnet bidirectional bridge destinations

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Configuration
PRIVATE_KEY="${PRIVATE_KEY:-}"
RPC_URL_138="${RPC_URL_138:-http://192.168.11.175:8545}"
RPC_URL_MAINNET="${RPC_URL_MAINNET:-https://eth.llamarpc.com}"

# Chain Selectors
MAINNET_CHAIN_SELECTOR="5009297550715157269"
CHAIN138_CHAIN_SELECTOR="${CHAIN138_CHAIN_SELECTOR:-}" # To be determined

# Contract Addresses (set these after deployment)
WETH9_BRIDGE_138="${WETH9_BRIDGE_138:-}"
WETH10_BRIDGE_138="${WETH10_BRIDGE_138:-}"
WETH9_MAINNET="${WETH9_MAINNET:-0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2}"
WETH10_MAINNET="${WETH10_MAINNET:-0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f}"

echo -e "${GREEN}=== Bridge Destination Configuration ===${NC}\n"

# Check prerequisites
if [ -z "$PRIVATE_KEY" ]; then
    echo -e "${RED}ERROR: PRIVATE_KEY environment variable not set${NC}"
    exit 1
fi

if [ -z "$WETH9_BRIDGE_138" ] || [ -z "$WETH10_BRIDGE_138" ]; then
    echo -e "${YELLOW}WARNING: Bridge addresses not set. Please set WETH9_BRIDGE_138 and WETH10_BRIDGE_138${NC}"
    exit 1
fi

# Function to add destination
add_destination() {
    local bridge_address=$1
    local chain_selector=$2
    local destination_token=$3
    local rpc_url=$4
    local network_name=$5

    echo -e "${GREEN}Adding destination to $network_name bridge...${NC}"
    
    cast send "$bridge_address" \
        "addDestination(uint64,address,address)" \
        "$chain_selector" \
        "$destination_token" \
        "0x0000000000000000000000000000000000000000" \
        --rpc-url "$rpc_url" \
        --private-key "$PRIVATE_KEY" \
        --legacy

    echo -e "${GREEN}✓ Destination added${NC}\n"
}

# Function to verify destinations
verify_destinations() {
    local bridge_address=$1
    local rpc_url=$2
    local network_name=$3

    echo -e "${GREEN}Verifying destinations on $network_name...${NC}"
    
    local chains=$(cast call "$bridge_address" \
        "getDestinationChains()(uint64[])" \
        --rpc-url "$rpc_url")
    
    echo -e "${GREEN}Destination chains: $chains${NC}\n"
}

# Configure ChainID 138 → Mainnet
echo -e "${YELLOW}=== Configuring ChainID 138 → Mainnet ===${NC}\n"

echo -e "${GREEN}1. Configuring WETH9 Bridge (ChainID 138 → Mainnet)${NC}"
add_destination "$WETH9_BRIDGE_138" "$MAINNET_CHAIN_SELECTOR" "$WETH9_MAINNET" "$RPC_URL_138" "ChainID 138"

echo -e "${GREEN}2. Configuring WETH10 Bridge (ChainID 138 → Mainnet)${NC}"
add_destination "$WETH10_BRIDGE_138" "$MAINNET_CHAIN_SELECTOR" "$WETH10_MAINNET" "$RPC_URL_138" "ChainID 138"

# Verify ChainID 138 bridges
echo -e "${YELLOW}=== Verifying ChainID 138 Bridges ===${NC}\n"
verify_destinations "$WETH9_BRIDGE_138" "$RPC_URL_138" "WETH9 Bridge"
verify_destinations "$WETH10_BRIDGE_138" "$RPC_URL_138" "WETH10 Bridge"

# Note: Mainnet → ChainID 138 requires ChainID 138 selector
if [ -z "$CHAIN138_CHAIN_SELECTOR" ]; then
    echo -e "${YELLOW}NOTE: ChainID 138 chain selector not set.${NC}"
    echo -e "${YELLOW}To configure Mainnet → ChainID 138, first determine the ChainID 138 selector.${NC}"
    echo -e "${YELLOW}You can find it using: cast call <CCIP_ROUTER> 'getChainSelector()(uint64)' --rpc-url $RPC_URL_138${NC}\n"
else
    echo -e "${YELLOW}=== Configuring Mainnet → ChainID 138 ===${NC}\n"
    echo -e "${YELLOW}NOTE: This requires deploying bridges on Mainnet first.${NC}\n"
fi

echo -e "${GREEN}=== Configuration Complete ===${NC}\n"
echo -e "Next Steps:"
echo -e "  1. Test bidirectional transfers"
echo -e "  2. Monitor bridge activity"
echo -e "  3. Verify LINK token balances for CCIP fees"
