#!/usr/bin/env bash
# CCIP Estimate Fee Script
# Estimates the CCIP fee for bridging tokens to a destination chain
#
# Usage:
#   ./ccip-estimate-fee.sh <chain-selector> <receiver-address> <amount-wei>
#
# Example:
#   export BRIDGE_ADDRESS=$CCIPWETH9_BRIDGE_CHAIN138
#   ./ccip-estimate-fee.sh 5009297550715157269 0xYourMainnetReceiverAddress 100000000000000000

set -euo pipefail

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

# Load environment variables
ENV_FILE="${ENV_FILE:-$PROJECT_ROOT/.env}"
if [ -f "$ENV_FILE" ]; then
    export $(grep -v '^#' "$ENV_FILE" | grep -E "^(RPC_URL|PRIVATE_KEY|CHAIN_ID|CCIP_ROUTER|LINK_TOKEN|WETH9_ADDRESS|WETH10_ADDRESS|FEE_TOKEN)" | xargs)
fi

# Parse arguments
if [ $# -lt 3 ]; then
    echo "Usage: $0 <chain-selector> <receiver-address> <amount-wei>"
    echo ""
    echo "Arguments:"
    echo "  chain-selector   CCIP chain selector (e.g., 5009297550715157269 for Ethereum mainnet)"
    echo "  receiver-address Address that will receive tokens on destination chain"
    echo "  amount-wei       Amount to bridge in wei (e.g., 100000000000000000 for 0.1 tokens)"
    echo ""
    echo "Environment variables:"
    echo "  BRIDGE_ADDRESS   Address of the bridge contract (required)"
    echo "  RPC_URL          RPC endpoint URL (required)"
    echo "  FEE_TOKEN        Fee token address (0x0 for native, or LINK token address)"
    echo ""
    echo "Examples:"
    echo "  # Estimate fee for 0.1 WETH9 from Chain 138 to Mainnet"
    echo "  export BRIDGE_ADDRESS=\$CCIPWETH9_BRIDGE_CHAIN138"
    echo "  $0 5009297550715157269 0xYourMainnetAddress 100000000000000000"
    exit 1
fi

CHAIN_SELECTOR="$1"
RECEIVER="$2"
AMOUNT="$3"

# Validate required environment variables
if [ -z "${BRIDGE_ADDRESS:-}" ]; then
    echo "Error: BRIDGE_ADDRESS not set"
    echo "Set it to the bridge contract address:"
    echo "  export BRIDGE_ADDRESS=\$CCIPWETH9_BRIDGE_CHAIN138"
    echo "  # or"
    echo "  export BRIDGE_ADDRESS=\$CCIPWETH10_BRIDGE_CHAIN138"
    exit 1
fi

if [ -z "${RPC_URL:-}" ]; then
    echo "Error: RPC_URL not set"
    exit 1
fi

# Default to LINK token if FEE_TOKEN not set
FEE_TOKEN="${FEE_TOKEN:-${LINK_TOKEN:-0x0000000000000000000000000000000000000000}}"

# Validate addresses
if ! cast --version &>/dev/null; then
    echo "Error: cast (Foundry) not found. Please install Foundry:"
    echo "  curl -L https://foundry.paradigm.xyz | bash"
    exit 1
fi

BRIDGE_ADDRESS=$(cast --to-checksum-address "$BRIDGE_ADDRESS")
RECEIVER=$(cast --to-checksum-address "$RECEIVER")

echo "=========================================="
echo "CCIP Estimate Fee"
echo "=========================================="
echo "Bridge Address: $BRIDGE_ADDRESS"
echo "Chain Selector: $CHAIN_SELECTOR"
echo "Receiver: $RECEIVER"
echo "Amount: $AMOUNT wei"
echo "Fee Token: $FEE_TOKEN"
echo ""

# Check if bridge has estimateCcipFee function
# If not, we'll need to call router.getFee directly
HAS_ESTIMATE_FUNCTION=$(cast sig "estimateCcipFee(uint64,address,uint256)(uint256)" 2>/dev/null || echo "")

if [ -n "$HAS_ESTIMATE_FUNCTION" ]; then
    echo "Using bridge's estimateCcipFee function..."
    FEE=$(cast call "$BRIDGE_ADDRESS" \
        "estimateCcipFee(uint64,address,uint256)(uint256)" \
        "$CHAIN_SELECTOR" \
        "$RECEIVER" \
        "$AMOUNT" \
        --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
else
    echo "Bridge doesn't have estimateCcipFee, calling router directly..."
    echo "⚠ Note: This requires constructing the full message, which may not be possible without bridge contract details"
    echo "Please implement estimateCcipFee in your bridge contract"
    exit 1
fi

if [ "$FEE" = "0" ] || [ -z "$FEE" ]; then
    echo "Error: Failed to estimate fee"
    exit 1
fi

# Convert to human-readable format
FEE_DECIMAL=$(cast --to-dec "$FEE" 2>/dev/null || echo "$FEE")

echo ""
echo "=========================================="
echo "Fee Estimate"
echo "=========================================="
echo "Fee (wei): $FEE"
echo "Fee (decimal): $FEE_DECIMAL"

# If using LINK token, show in LINK units (18 decimals)
if [ "$FEE_TOKEN" != "0x0000000000000000000000000000000000000000" ]; then
    FEE_LINK=$(echo "scale=8; $FEE_DECIMAL / 1000000000000000000" | bc 2>/dev/null || echo "N/A")
    echo "Fee (LINK): $FEE_LINK LINK"
else
    FEE_ETH=$(echo "scale=8; $FEE_DECIMAL / 1000000000000000000" | bc 2>/dev/null || echo "N/A")
    echo "Fee (ETH): $FEE_ETH ETH"
fi

echo ""
echo "=========================================="

