#!/usr/bin/env bash
set -euo pipefail

if [[ "${CI:-}" == "true" ]]; then
  # ── CI path ────────────────────────────────────────────────────────
  # NOTE: this block is NOT executed by the .woodpecker/e2e.yml pipeline.
  # Woodpecker runs the webapp image as a service with a `commands:` block,
  # which replaces the Docker ENTRYPOINT entirely — dumb-init and this script
  # are bypassed. The e2e.yml commands block sources contracts.env and starts
  # `npm run dev` inline (see .woodpecker/e2e.yml, webapp service, ~line 129).
  #
  # This path fires only for manual invocations, e.g.:
  #   docker run -e CI=true \
  #     -e VITE_KRAIKEN_ADDRESS=0x... \
  #     -e VITE_STAKE_ADDRESS=0x... \
  #     webapp-ci
  #
  # VITE_KRAIKEN_ADDRESS and VITE_STAKE_ADDRESS must be supplied by the caller;
  # they are not derived from contracts.env here.
  cd /app/web-app

  echo "[webapp-ci] Starting Web App..."

  : "${VITE_KRAIKEN_ADDRESS:?VITE_KRAIKEN_ADDRESS must be supplied by the caller (not sourced from contracts.env in this path)}"
  : "${VITE_STAKE_ADDRESS:?VITE_STAKE_ADDRESS must be supplied by the caller (not sourced from contracts.env in this path)}"

  # Disable Vue DevTools in CI to avoid path resolution issues
  export CI=true
  export VITE_DEFAULT_CHAIN_ID=${VITE_DEFAULT_CHAIN_ID:-31337}
  export VITE_LOCAL_RPC_URL=${VITE_LOCAL_RPC_URL:-/api/rpc}
  export VITE_LOCAL_RPC_PROXY_TARGET=${VITE_LOCAL_RPC_PROXY_TARGET:-http://anvil:8545}
  export VITE_LOCAL_GRAPHQL_PROXY_TARGET=${VITE_LOCAL_GRAPHQL_PROXY_TARGET:-http://ponder:42069}
  export VITE_LOCAL_TXN_PROXY_TARGET=${VITE_LOCAL_TXN_PROXY_TARGET:-http://txn-bot:43069}
  export VITE_SWAP_ROUTER=${VITE_SWAP_ROUTER:-0x94cC0AaC535CCDB3C01d6787D6413C739ae12bc4}
  export VITE_PONDER_BASE_SEPOLIA_LOCAL_FORK=${VITE_PONDER_BASE_SEPOLIA_LOCAL_FORK:-/api/graphql}
  export VITE_TXNBOT_BASE_SEPOLIA_LOCAL_FORK=${VITE_TXNBOT_BASE_SEPOLIA_LOCAL_FORK:-/api/txn}
  export VITE_UMAMI_URL="${VITE_UMAMI_URL:-}"
  export VITE_UMAMI_WEBSITE_ID="${VITE_UMAMI_WEBSITE_ID:-}"

  echo "[webapp-ci] Environment configured:"
  echo "  VITE_KRAIKEN_ADDRESS: ${VITE_KRAIKEN_ADDRESS}"
  echo "  VITE_STAKE_ADDRESS: ${VITE_STAKE_ADDRESS}"
  echo "  VITE_DEFAULT_CHAIN_ID: ${VITE_DEFAULT_CHAIN_ID}"

  exec npm run dev -- --host 0.0.0.0 --port 5173 --base /app/
fi

# ── Local dev path ─────────────────────────────────────────────────
ROOT_DIR=/workspace
# Default is the Sepolia SwapRouter; override via SWAP_ROUTER env var for other networks.
SWAP_ROUTER=${SWAP_ROUTER:-0x94cC0AaC535CCDB3C01d6787D6413C739ae12bc4}

# shellcheck source=entrypoint-common.sh
source "$ROOT_DIR/containers/entrypoint-common.sh"

entrypoint_checkout_branch "$ROOT_DIR" "webapp-entrypoint"

CONTRACT_ENV=$ROOT_DIR/tmp/containers/contracts.env
APP_DIR=$ROOT_DIR/web-app

while [[ ! -f "$CONTRACT_ENV" ]]; do
  echo "[webapp-entrypoint] waiting for contracts env"
  sleep 2
done

entrypoint_require_kraiken_lib "$ROOT_DIR" "webapp-entrypoint"

# shellcheck disable=SC1090
source "$CONTRACT_ENV"

cd "$APP_DIR"

entrypoint_install_deps "webapp-entrypoint"

export VITE_DEFAULT_CHAIN_ID=${VITE_DEFAULT_CHAIN_ID:-31337}
export VITE_LOCAL_RPC_URL=${VITE_LOCAL_RPC_URL:-/api/rpc}
export VITE_LOCAL_RPC_PROXY_TARGET=${VITE_LOCAL_RPC_PROXY_TARGET:-http://anvil:8545}
export VITE_LOCAL_GRAPHQL_PROXY_TARGET=${VITE_LOCAL_GRAPHQL_PROXY_TARGET:-http://ponder:42069}
export VITE_LOCAL_TXN_PROXY_TARGET=${VITE_LOCAL_TXN_PROXY_TARGET:-http://txn-bot:43069}
export VITE_KRAIKEN_ADDRESS=$KRAIKEN
export VITE_STAKE_ADDRESS=$STAKE
export VITE_SWAP_ROUTER=$SWAP_ROUTER
export VITE_PONDER_BASE_SEPOLIA_LOCAL_FORK=${VITE_PONDER_BASE_SEPOLIA_LOCAL_FORK:-/api/graphql}
export VITE_TXNBOT_BASE_SEPOLIA_LOCAL_FORK=${VITE_TXNBOT_BASE_SEPOLIA_LOCAL_FORK:-/api/txn}
export CHOKIDAR_USEPOLLING=${CHOKIDAR_USEPOLLING:-1}
export VITE_UMAMI_URL="${VITE_UMAMI_URL:-}"
export VITE_UMAMI_WEBSITE_ID="${VITE_UMAMI_WEBSITE_ID:-}"

exec npm run dev -- --host 0.0.0.0 --port 5173 --base /app/
