#!/usr/bin/env bash
# Fund all CCIP WETH9/WETH10 bridge contracts with LINK on each chain.
# Amount via tag (not .env): --link <amount> (default 10 LINK), --dry-run to print commands only.
# Usage: ./scripts/deployment/fund-ccip-bridges-with-link.sh [--link 10] [--dry-run]

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

source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
source "$SCRIPT_DIR/../lib/deployment/prompts.sh"
load_deployment_env
parse_link_tags "$@"

[[ -f "$SCRIPT_DIR/../lib/infura.sh" ]] && source "$SCRIPT_DIR/../lib/infura.sh" 2>/dev/null || true
[[ -n "${PRIVATE_KEY:-}" && ! "$PRIVATE_KEY" =~ ^0x ]] && PRIVATE_KEY="0x$PRIVATE_KEY"

run_or_echo() {
  if [[ "${DRY_RUN:-0}" = "1" ]]; then
    echo "  [DRY RUN] $*"
  else
    local _out
    if _out=$(eval "$*" 2>&1); then
      echo "  OK"
      [[ -n "$_out" ]] && echo "    $_out"
    else
      echo "  Failed (non-fatal)" >&2
      echo "    $_out" >&2
    fi
  fi
}

ensure_rpc() { local rpc="$1"; type ensure_infura_rpc_url &>/dev/null && [[ -n "$rpc" ]] && rpc=$(ensure_infura_rpc_url "$rpc"); echo "$rpc"; }

if [[ -z "$PRIVATE_KEY" ]]; then
  echo "ERROR: PRIVATE_KEY not set" >&2
  exit 1
fi

echo "Funding CCIP bridges with LINK (amount per bridge: $LINK_AMOUNT_WEI wei)"
echo ""

# Chain 138 (Besu: gas estimation often fails; use explicit legacy gas like manual cast)
if [[ -n "${RPC_URL_138:-}" && -n "${LINK_TOKEN_CHAIN138:-${LINK_TOKEN:-}}" ]]; then
  link="${LINK_TOKEN_CHAIN138:-$LINK_TOKEN}"
  link="${link,,}"
  rpc=$(ensure_rpc "$RPC_URL_138")
  echo "Chain 138 (RPC: ${rpc%%\?*}...)"
  _gas138="--legacy --gas-limit 250000 --gas-price 2000000000"
  [[ -n "${CCIPWETH9_BRIDGE_CHAIN138:-}" ]]  && run_or_echo "cast send $link \"transfer(address,uint256)\" ${CCIPWETH9_BRIDGE_CHAIN138,,} $LINK_AMOUNT_WEI --rpc-url \"$rpc\" --private-key \"\$PRIVATE_KEY\" $_gas138"
  [[ -n "${CCIPWETH10_BRIDGE_CHAIN138:-}" ]] && run_or_echo "cast send $link \"transfer(address,uint256)\" ${CCIPWETH10_BRIDGE_CHAIN138,,} $LINK_AMOUNT_WEI --rpc-url \"$rpc\" --private-key \"\$PRIVATE_KEY\" $_gas138"
  echo ""
fi

# Ethereum
if [[ -n "${ETHEREUM_MAINNET_RPC:-}" && -n "${MAINNET_LINK_TOKEN:-${CCIP_ETH_LINK_TOKEN:-}}" ]]; then
  link="${MAINNET_LINK_TOKEN:-$CCIP_ETH_LINK_TOKEN}"
  rpc=$(ensure_rpc "$ETHEREUM_MAINNET_RPC")
  echo "Ethereum Mainnet"
  [[ -n "${MAINNET_CCIP_WETH9_BRIDGE:-}" ]]  && run_or_echo "cast send $link \"transfer(address,uint256)\" $MAINNET_CCIP_WETH9_BRIDGE $LINK_AMOUNT_WEI --rpc-url \"$rpc\" --private-key \"\$PRIVATE_KEY\" --legacy"
  [[ -n "${MAINNET_CCIP_WETH10_BRIDGE:-}" ]] && run_or_echo "cast send $link \"transfer(address,uint256)\" $MAINNET_CCIP_WETH10_BRIDGE $LINK_AMOUNT_WEI --rpc-url \"$rpc\" --private-key \"\$PRIVATE_KEY\" --legacy"
  echo ""
fi

# BSC, Polygon, Base, Optimism, Arbitrum, Avalanche, Cronos, Gnosis, Celo, Wemix (matches check-link-balance-config-ready-chains.sh)
for label in BSC POLYGON BASE OPTIMISM ARBITRUM AVALANCHE CRONOS GNOSIS CELO WEMIX; do
  case "$label" in
    BSC)      rpc_var="BSC_RPC_URL";              link_var="CCIP_BSC_LINK_TOKEN"; ;;
    POLYGON)  rpc_var="POLYGON_MAINNET_RPC";      link_var="CCIP_POLYGON_LINK_TOKEN"; ;;
    BASE)     rpc_var="BASE_MAINNET_RPC";         link_var="CCIP_BASE_LINK_TOKEN"; ;;
    OPTIMISM) rpc_var="OPTIMISM_MAINNET_RPC";     link_var="CCIP_OPTIMISM_LINK_TOKEN"; ;;
    ARBITRUM) rpc_var="ARBITRUM_MAINNET_RPC";     link_var="CCIP_ARBITRUM_LINK_TOKEN"; ;;
    AVALANCHE) rpc_var="AVALANCHE_RPC_URL";      link_var="CCIP_AVALANCHE_LINK_TOKEN"; ;;
    CRONOS)   rpc_var="CRONOS_RPC_URL";           link_var="CCIP_CRONOS_LINK_TOKEN"; rpc_fb="CRONOS_RPC"; link_fb="LINK_TOKEN_CRONOS"; ;;
    GNOSIS)   rpc_var="GNOSIS_MAINNET_RPC";       link_var="CCIP_GNOSIS_LINK_TOKEN"; rpc_fb="GNOSIS_RPC"; link_fb="LINK_TOKEN_GNOSIS"; ;;
    CELO)     rpc_var="CELO_MAINNET_RPC";         link_var="CCIP_CELO_LINK_TOKEN";   rpc_fb="CELO_RPC"; link_fb="LINK_TOKEN_CELO"; ;;
    WEMIX)    rpc_var="WEMIX_RPC";                link_var="CCIP_WEMIX_LINK_TOKEN";  rpc_fb="WEMIX_MAINNET_RPC"; link_fb="LINK_TOKEN_WEMIX"; ;;
  esac
  rpc="${!rpc_var:-${!rpc_fb:-}}"
  link="${!link_var:-${!link_fb:-}}"
  rpc=$(ensure_rpc "$rpc")
  [[ -z "$rpc" || -z "$link" ]] && continue

  bridge9_var="CCIPWETH9_BRIDGE_${label}"
  bridge10_var="CCIPWETH10_BRIDGE_${label}"
  addr9="${!bridge9_var:-}"
  addr10="${!bridge10_var:-}"
  [[ -z "$addr9" && -z "$addr10" ]] && continue

  echo "$label"
  [[ -n "$addr9" ]]  && run_or_echo "cast send ${link,,} \"transfer(address,uint256)\" ${addr9,,}  $LINK_AMOUNT_WEI --rpc-url \"$rpc\" --private-key \"\$PRIVATE_KEY\" --legacy"
  [[ -n "$addr10" ]] && run_or_echo "cast send ${link,,} \"transfer(address,uint256)\" ${addr10,,} $LINK_AMOUNT_WEI --rpc-url \"$rpc\" --private-key \"\$PRIVATE_KEY\" --legacy"
  echo ""
done

echo "Done. Use --link <amount> and --dry-run to adjust."
