#!/usr/bin/env bash
# Check deployer LINK balance on Gnosis, Cronos, Celo, Wemix (config-ready chains).
# Required before fund-ccip-bridges-with-link.sh: 20 LINK per chain (10 per bridge).
#
# Usage: ./scripts/deployment/check-link-balance-config-ready-chains.sh [gnosis|cronos|celo|wemix|all]

set -euo pipefail

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

if [[ -f "$SCRIPT_DIR/../lib/deployment/dotenv.sh" ]]; then
  # shellcheck disable=SC1090
  source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "$PROJECT_ROOT"
elif [[ -f "$PROJECT_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$PROJECT_ROOT/.env"
  set +a
fi

CHAIN="${1:-all}"
REQUIRED_LINK="20"  # 10 per WETH9 bridge + 10 per WETH10 bridge

# LINK token addresses (from CCIP directory)
LINK_TOKEN_GNOSIS="${LINK_TOKEN_GNOSIS:-${CCIP_GNOSIS_LINK_TOKEN:-0xE2e73A1c69ecF83F464EFCE6A5be353a37cA09b2}}"
LINK_TOKEN_CRONOS="${LINK_TOKEN_CRONOS:-${CCIP_CRONOS_LINK_TOKEN:-0x8c80A01F461f297Df7F9DA3A4f740D7297C8Ac85}}"
LINK_TOKEN_CELO="${LINK_TOKEN_CELO:-${CCIP_CELO_LINK_TOKEN:-0xd07294e6E917e07dfDcee882dd1e2565085C2ae0}}"
LINK_TOKEN_WEMIX="${LINK_TOKEN_WEMIX:-${CCIP_WEMIX_LINK_TOKEN:-0x80f1FcdC96B55e459BF52b998aBBE2c364935d69}}"

GNOSIS_RPC="${GNOSIS_RPC:-${GNOSIS_MAINNET_RPC:-https://rpc.gnosischain.com}}"
CRONOS_RPC="${CRONOS_RPC:-${CRONOS_RPC_URL:-https://evm.cronos.org}}"
CELO_RPC="${CELO_RPC:-${CELO_MAINNET_RPC:-https://forno.celo.org}}"
WEMIX_RPC="${WEMIX_RPC:-${WEMIX_MAINNET_RPC:-https://api.wemix.com}}"

if [[ -z "${PRIVATE_KEY:-}" ]]; then
  echo "Error: Set PRIVATE_KEY in .env" >&2
  exit 1
fi
DEPLOYER=$(cast wallet address "$PRIVATE_KEY" 2>/dev/null || true)
[[ -z "$DEPLOYER" ]] && { echo "Error: Could not derive deployer from PRIVATE_KEY" >&2; exit 1; }

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

check_link() {
  local name="$1" rpc="$2" link_token="$3"
  [[ -z "$rpc" || -z "$link_token" ]] && return 1
  local raw
  raw=$(cast call "${link_token,,}" "balanceOf(address)(uint256)" "${DEPLOYER,,}" --rpc-url "$rpc" 2>/dev/null || echo "0")
  local link_val
  link_val=$(echo "scale=4; $raw / 1000000000000000000" | bc 2>/dev/null || echo "0")
  if [[ -z "$link_val" || "$link_val" == "0" ]]; then
    echo -e "  ${RED}✗${NC} $name: 0 LINK (need $REQUIRED_LINK LINK)"
    return 1
  fi
  if [[ "$(echo "$link_val >= $REQUIRED_LINK" | bc 2>/dev/null)" == "1" ]]; then
    echo -e "  ${GREEN}✓${NC} $name: $link_val LINK"
    return 0
  fi
  echo -e "  ${YELLOW}⚠${NC} $name: $link_val LINK (need $REQUIRED_LINK LINK)"
  return 1
}

echo "=== LINK Balance Check (Config-Ready Chains) ==="
echo "Deployer: ${DEPLOYER:0:10}...${DEPLOYER: -8}"
echo "Required: $REQUIRED_LINK LINK per chain (10 per WETH9 + 10 per WETH10 bridge)"
echo ""

failed=0
case "$CHAIN" in
  gnosis) check_link "Gnosis" "$GNOSIS_RPC" "$LINK_TOKEN_GNOSIS" || failed=1 ;;
  cronos) check_link "Cronos" "$CRONOS_RPC" "$LINK_TOKEN_CRONOS" || failed=1 ;;
  celo)   check_link "Celo"   "$CELO_RPC"   "$LINK_TOKEN_CELO"   || failed=1 ;;
  wemix)  check_link "Wemix"  "$WEMIX_RPC"  "$LINK_TOKEN_WEMIX"  || failed=1 ;;
  all)
    check_link "Gnosis" "$GNOSIS_RPC" "$LINK_TOKEN_GNOSIS" || failed=1
    check_link "Cronos" "$CRONOS_RPC" "$LINK_TOKEN_CRONOS" || failed=1
    check_link "Celo"   "$CELO_RPC"   "$LINK_TOKEN_CELO"   || failed=1
    check_link "Wemix"  "$WEMIX_RPC"  "$LINK_TOKEN_WEMIX"  || failed=1
    ;;
  *) echo "Usage: $0 [gnosis|cronos|celo|wemix|all]"; exit 1 ;;
esac

echo ""
if [[ $failed -eq 0 ]]; then
  echo "LINK balance OK. Run: ./scripts/deployment/fund-ccip-bridges-with-link.sh"
  exit 0
else
  echo "Acquire LINK on each chain (bridge from mainnet or DEX), then re-run this check."
  echo "  Gnosis: $LINK_TOKEN_GNOSIS"
  echo "  Cronos: $LINK_TOKEN_CRONOS"
  echo "  Celo:   $LINK_TOKEN_CELO"
  echo "  Wemix:  $LINK_TOKEN_WEMIX"
  exit 1
fi
