#!/usr/bin/env bash

set -euo pipefail

ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT_DIR"

MODE="default"
STEP_BY_STEP=0
VERBOSE=0
RESET=0

while [[ $# -gt 0 ]]; do
  case "$1" in
    --live)
      MODE="live"
      shift
      ;;
    --reset)
      RESET=1
      shift
      ;;
    --step-by-step|-i)
      STEP_BY_STEP=1
      VERBOSE=1
      shift
      ;;
    --verbose|-v)
      VERBOSE=1
      shift
      ;;
    *)
      printf 'usage: %s [--live] [--reset] [--step-by-step|-i] [--verbose|-v]\n' "$0" >&2
      exit 1
      ;;
  esac
done

if [[ -f "$ROOT_DIR/.env" ]]; then
  set -a
  # shellcheck disable=SC1091
  source "$ROOT_DIR/.env"
  set +a
fi

if [[ "$MODE" == "live" ]]; then
  missing=()
  for key in RHO_TELEGRAM_PROFILE RHO_TELEGRAM_BOT_TOKEN RHO_TELEGRAM_CHAT_ID RHO_TELEGRAM_USER_ID; do
    if [[ -z "${!key:-}" ]]; then
      missing+=("$key")
    fi
  done
  if (( ${#missing[@]} > 0 )); then
    printf 'missing live test env vars: %s\n' "${missing[*]}" >&2
    printf 'copy .env.example to .env and fill in the Telegram values\n' >&2
    exit 1
  fi
fi

export RHO_SCENARIO_MODE="$MODE"

ARGS=()
if (( STEP_BY_STEP )); then
  ARGS+=(--step-by-step)
fi
if (( VERBOSE )); then
  ARGS+=(--verbose)
fi
if (( RESET )); then
  ARGS+=(--reset)
fi

exec python3 "$ROOT_DIR/tests/run_yaml_scenario.py" \
  "${ARGS[@]}" \
  "$ROOT_DIR/tests/scenarios/two-console-demo/scenario.yaml"
