#!/usr/bin/env bash
# Fee management and optimization for bridge operations
# Usage: ./fee-management.sh [check|estimate|alert]

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_PROJECT="/home/intlc/projects/smom-dbis-138"

source "$SOURCE_PROJECT/.env" 2>/dev/null || true

RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
WETH9_BRIDGE="${CCIPWETH9_BRIDGE_CHAIN138:-0x89dd12025bfCD38A168455A44B400e913ED33BE2}"
LINK_TOKEN="${LINK_TOKEN_CHAIN138:-0x326C977E6efc84E512bB9C30f76E30c160eD06FB}"
ALERT_THRESHOLD=1.0  # Alert if fees exceed 1 ETH equivalent

ACTION="${1:-check}"

# Check LINK token balance
check_link_balance() {
    DEPLOYER=$(cast wallet address --private-key "$PRIVATE_KEY" 2>/dev/null || echo "")
    if [ -z "$DEPLOYER" ]; then
        echo "ERROR: Cannot determine deployer address"
        return 1
    fi
    
    LINK_BAL=$(cast call "$LINK_TOKEN" "balanceOf(address)" "$DEPLOYER" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
    LINK_BAL_ETH=$(echo "scale=6; $LINK_BAL / 1000000000000000000" | bc 2>/dev/null || echo "0")
    
    echo "LINK Balance: $LINK_BAL_ETH LINK"
    
    if (( $(echo "$LINK_BAL_ETH < 0.1" | bc -l 2>/dev/null || echo 1) )); then
        echo "⚠️  WARNING: Low LINK balance. May need to add more for fees."
    fi
}

# Estimate fees for all chains
estimate_fees() {
    local amount_wei="${1:-1000000000000000000}"  # 1 ETH default
    
    echo "=== Fee Estimation ==="
    echo "Amount: $(echo "scale=4; $amount_wei / 1000000000000000000" | bc) ETH"
    echo ""
    
    declare -A CHAINS=(
        ["BSC"]="11344663589394136015"
        ["Polygon"]="4051577828743386545"
        ["Avalanche"]="6433500567565415381"
        ["Base"]="15971525489660198786"
        ["Arbitrum"]="4949039107694359620"
        ["Optimism"]="3734403246176062136"
        ["Ethereum"]="5009297550715157269"
    )
    
    local total_fees=0
    for chain in "${!CHAINS[@]}"; do
        selector="${CHAINS[$chain]}"
        fee=$(cast call "$WETH9_BRIDGE" "calculateFee(uint64,uint256)" "$selector" "$amount_wei" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
        if [ "$fee" != "0" ]; then
            fee_eth=$(echo "scale=6; $fee / 1000000000000000000" | bc 2>/dev/null || echo "0")
            echo "$chain: $fee_eth ETH"
            total_fees=$(echo "$total_fees + $fee" | bc)
        fi
    done
    
    total_fees_eth=$(echo "scale=6; $total_fees / 1000000000000000000" | bc 2>/dev/null || echo "0")
    echo ""
    echo "Total Fees (all chains): $total_fees_eth ETH"
}

# Check fees and alert if high
check_fees() {
    check_link_balance
    echo ""
    estimate_fees
    
    # Check if fees exceed threshold
    local test_amount=$(cast --to-wei 1.0 ether 2>/dev/null)
    local bsc_fee=$(cast call "$WETH9_BRIDGE" "calculateFee(uint64,uint256)" "11344663589394136015" "$test_amount" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
    local bsc_fee_eth=$(echo "scale=6; $bsc_fee / 1000000000000000000" | bc 2>/dev/null || echo "0")
    
    if (( $(echo "$bsc_fee_eth > $ALERT_THRESHOLD" | bc -l 2>/dev/null || echo 0) )); then
        echo ""
        echo "⚠️  ALERT: Fees exceed threshold ($ALERT_THRESHOLD ETH)"
    fi
}

case "$ACTION" in
    check) check_fees ;;
    estimate) estimate_fees "${2:-1000000000000000000}" ;;
    alert) check_fees ;;
    *)
        echo "Usage: $0 [check|estimate|alert]"
        exit 1
        ;;
esac

