#!/usr/bin/env bash
# Print cast commands for live trustless bridge test (138 -> mainnet) and optionally check LP/BondManager state.
# Does NOT send the lock tx; you run that and capture depositId from the Deposit event.
# Usage: ./scripts/deployment/live-test-trustless-bridge.sh [--check]
#   --check  Print LP getPoolStats(0) and BondManager totalEthHeld() for mainnet.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$REPO_ROOT"
# Load .env via dotenv (RPC CR/LF trim). Fallback: raw source.
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:-$REPO_ROOT}"
elif [[ -n "${PROJECT_ROOT:-}" && -f "$PROJECT_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$PROJECT_ROOT/.env"
  set +a
elif [[ -n "${REPO_ROOT:-}" && -f "$REPO_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$REPO_ROOT/.env"
  set +a
fi

BOND_MANAGER="${BOND_MANAGER_MAINNET:-${BOND_MANAGER:-}}"
CHALLENGE_MANAGER="${CHALLENGE_MANAGER_MAINNET:-${CHALLENGE_MANAGER:-}}"
LIQUIDITY_POOL="${LIQUIDITY_POOL_ETH_MAINNET:-${LIQUIDITY_POOL:-}}"
INBOX_ETH="${INBOX_ETH_MAINNET:-${INBOX_ETH:-}}"
RPC_138="${RPC_URL_138:-${CHAIN_138_RPC_URL:-}}"
RPC_MAINNET="${ETHEREUM_MAINNET_RPC:-}"

if [[ "${1:-}" == "--check" ]]; then
  echo "--- Mainnet LP ETH pool stats (AssetType 0) ---"
  cast call "$LIQUIDITY_POOL" "getPoolStats(uint8)(uint256,uint256,uint256)" 0 --rpc-url "$RPC_MAINNET" 2>/dev/null || echo " (set LIQUIDITY_POOL and ETHEREUM_MAINNET_RPC)"
  echo "--- BondManager totalEthHeld ---"
  cast call "$BOND_MANAGER" "totalEthHeld()(uint256)" --rpc-url "$RPC_MAINNET" 2>/dev/null || echo " (set BOND_MANAGER and ETHEREUM_MAINNET_RPC)"
  echo ""
fi

echo "=== Live test commands (138 -> mainnet) ==="
echo "Set: RECIPIENT (mainnet address), DEPOSIT_ID (from Lockbox Deposit event depositId — NOT the amount), AMOUNT_WEI (e.g. 1000000000000000 for 0.001 ether)"
echo ""
echo "Get DEPOSIT_ID after lock: ./scripts/deployment/run-e2e-trustless-live-test.sh get-deposit-id <LOCK_TX_HASH>   # or cast logs <TX> \"Deposit(...)\" --rpc-url \$RPC_URL_138"
echo ""
echo "1) Lock on Chain 138 (run this; then get DEPOSIT_ID from Deposit event):"
echo "   cast send \$LOCKBOX_138 \"depositNative(address,bytes32)\" \$RECIPIENT \$(cast keccak \"live-test-\$(date +%s)\") --value 0.01ether --rpc-url \$RPC_URL_138 --private-key \$PRIVATE_KEY --legacy --gas-price \${GAS_PRICE_138:-1000000000}"
echo ""
echo "2) Bond for claim (run and set BOND=...):"
echo "   BOND=\$(cast call \$BOND_MANAGER \"getRequiredBond(uint256)\" \$AMOUNT_WEI --rpc-url \$ETHEREUM_MAINNET_RPC)"
echo ""
echo "3) Submit claim on mainnet:"
echo "   cast send \$INBOX_ETH \"submitClaim(uint256,address,uint256,address,bytes)\" \$DEPOSIT_ID 0x0000000000000000000000000000000000000000 \$AMOUNT_WEI \$RECIPIENT 0x --value \$BOND --rpc-url \$ETHEREUM_MAINNET_RPC --private-key \$PRIVATE_KEY"
echo ""
echo "4) After challenge window (e.g. 30 min), finalize:"
echo "   cast send \$CHALLENGE_MANAGER \"finalizeClaim(uint256)\" \$DEPOSIT_ID --rpc-url \$ETHEREUM_MAINNET_RPC --private-key \$PRIVATE_KEY  # CHALLENGE_MANAGER in .env"
echo ""
echo "5) Release bond:"
echo "   cast send \$BOND_MANAGER \"releaseBond(uint256)\" \$DEPOSIT_ID --rpc-url \$ETHEREUM_MAINNET_RPC --private-key \$PRIVATE_KEY"
echo ""
echo "Full runbook: docs/bridge/trustless/DEPLOYMENT_GUIDE.md (Deploy, Configure, and Live Test section)"
