#!/usr/bin/env bash
# Fund the mainnet LiquidityPoolETH with ETH and/or WETH (G4).
# Uses .env for PRIVATE_KEY, LIQUIDITY_POOL_ETH_MAINNET (or LIQUIDITY_POOL), ETHEREUM_MAINNET_RPC.
# Amounts via tags (not .env): --eth <amount>, --weth <amount> (in ETH), or --eth-wei / --weth-wei.
# If no amounts given and stdin is a TTY, prompts interactively.
#
# Usage:
#   ./scripts/deployment/fund-mainnet-lp.sh --eth 1.5 --weth 0.5
#   ./scripts/deployment/fund-mainnet-lp.sh --eth 1
#   ./scripts/deployment/fund-mainnet-lp.sh   # interactive prompt
#   ./scripts/deployment/fund-mainnet-lp.sh --dry-run  # no tx, show what would be sent
#
# Callable from other scripts: pass same tags; or source and call run_fund_mainnet_lp "${args[@]}"
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$REPO_ROOT"

# Load deployment env and tag parsing
source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
source "$SCRIPT_DIR/../lib/deployment/prompts.sh"
load_deployment_env

# Parse tags; remaining args left in PARSE_FUND_TAGS_REMAINING
parse_fund_tags "$@"

# Interactive if no amounts and TTY
prompt_fund_amounts

if [[ "${FUND_ETH_AMOUNT_WEI:-0}" == "0" && "${FUND_WETH_AMOUNT_WEI:-0}" == "0" ]]; then
  echo "No amounts set. Use --eth <amount> and/or --weth <amount>, or run interactively."
  echo "Example: $0 --eth 1.5 --weth 0.5"
  exit 0
fi

require_fund_lp_env || exit 1

if [[ "${DRY_RUN:-0}" == "1" ]]; then
  echo "Dry run: would fund LP with ETH wei=$FUND_ETH_AMOUNT_WEI, WETH wei=$FUND_WETH_AMOUNT_WEI"
  exit 0
fi

echo "Funding mainnet LP at $LIQUIDITY_POOL_ETH_MAINNET (ETH wei=$FUND_ETH_AMOUNT_WEI, WETH wei=$FUND_WETH_AMOUNT_WEI)"
FUND_ETH_AMOUNT_WEI="$FUND_ETH_AMOUNT_WEI" FUND_WETH_AMOUNT_WEI="$FUND_WETH_AMOUNT_WEI" \
  forge script script/bridge/trustless/FundMainnetLP.s.sol:FundMainnetLP \
  --rpc-url "$ETHEREUM_MAINNET_RPC" \
  --broadcast \
  --private-key "$PRIVATE_KEY" \
  -vvv
