#!/usr/bin/env bash
# Run liquidity-gap tasks (G1–G4) using .env. Phase and G4 amounts via tags (not .env).
#
# Usage:
#   ./scripts/deployment/run-all-four-gaps.sh                    # run all phases (G4 interactive if no amounts)
#   ./scripts/deployment/run-all-four-gaps.sh g1 g4              # run only G1 and G4
#   ./scripts/deployment/run-all-four-gaps.sh g4 --eth 1 --weth 0.5   # G4 with amounts (no prompt)
#   ./scripts/deployment/run-all-four-gaps.sh g4                  # G4 with interactive prompt
#
# Phases: g1 (PMM on L2s), g2g3 (Trustless + Lockbox on L2s), g4 (fund mainnet LP).
# G4 amounts: use --eth, --weth, --eth-wei, --weth-wei (see fund-mainnet-lp.sh).
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$REPO_ROOT"
source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
load_deployment_env
DOTENV="${ENV_FILE:-$PROJECT_ROOT/.env}"
if [[ ! -f "$DOTENV" ]]; then
  echo "No .env at $DOTENV. Create from .env.example and set PRIVATE_KEY, RPCs, token addresses. Or set ENV_FILE."
  exit 1
fi

# Parse phase tags (g1, g2g3, g4); remaining args may include G2G3 tags (--lockbox, --chain) and G4 tags (--eth, --weth)
source "$SCRIPT_DIR/../lib/deployment/prompts.sh"
parse_phase_tags "$@"
REMAINING=("${PARSE_PHASE_TAGS_REMAINING[@]}")
parse_lockbox_tag "${REMAINING[@]}"
parse_chain_filter "${PARSE_LOCKBOX_REMAINING[@]}"
G4_ARGS=("${PARSE_CHAIN_FILTER_REMAINING[@]}")
G2G3_ARGS=()
[[ "${TRUSTLESS_DEPLOY_LOCKBOX:-1}" == "1" ]] && G2G3_ARGS+=(--lockbox) || G2G3_ARGS+=(--no-lockbox)
[[ ${#CHAIN_FILTER[@]} -gt 0 ]] && G2G3_ARGS+=(--chain "${CHAIN_FILTER[@]}")

echo "=============================================="
echo "Running liquidity gaps (secrets from .env)"
echo "=============================================="

if [[ "$RUN_G1" == "1" ]]; then
  echo ""
  echo "--- G1: Deploy PMM on L2s ---"
  ./scripts/deployment/deploy-pmm-all-l2s.sh || true
fi

if [[ "$RUN_G2G3" == "1" ]]; then
  echo ""
  echo "--- G2/G3: Deploy Trustless (+ Lockbox) on L2s ---"
  ./scripts/deployment/deploy-trustless-l2s.sh "${G2G3_ARGS[@]}" || true
fi

if [[ "$RUN_G4" == "1" ]]; then
  echo ""
  echo "--- G4: Fund mainnet LP ---"
  ./scripts/deployment/fund-mainnet-lp.sh "${G4_ARGS[@]}" || true
fi

echo ""
echo "Done. Update .env with any new contract addresses printed above."
