#!/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
AGENT_FIXTURE_MODE="${RHO_AGENT_FIXTURE_MODE:-online}"
AGENT_FIXTURE_ROOT="${RHO_AGENT_FIXTURE_ROOT:-$ROOT_DIR/tests/fixtures/two-console-demo-agent-approval/agent-run}"
MIDDLEWARE_ADAPTER="${RHO_MIDDLEWARE_ADAPTER:-}"
MIDDLEWARE_FIXTURE_FILE="${RHO_MIDDLEWARE_FIXTURE_FILE:-}"
SCENARIO_DATASET_UUID="${RHO_SCENARIO_GEN_DATASET_UUID:-11111111-1111-1111-1111-111111111111}"

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
      ;;
    --offline)
      AGENT_FIXTURE_MODE="replay"
      shift
      ;;
    --record-fixtures)
      AGENT_FIXTURE_MODE="record"
      shift
      ;;
    --middleware)
      MIDDLEWARE_ADAPTER="${2:-}"
      shift 2
      ;;
    --middleware-fixture)
      MIDDLEWARE_FIXTURE_FILE="${2:-}"
      shift 2
      ;;
    *)
      printf 'usage: %s [--live] [--reset] [--step-by-step|-i] [--verbose|-v] [--offline] [--record-fixtures] [--middleware auto|telegram|fixture] [--middleware-fixture path]\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 [[ -z "$MIDDLEWARE_ADAPTER" ]]; then
  if [[ "$MODE" == "live" ]]; then
    MIDDLEWARE_ADAPTER="telegram"
  elif [[ "$AGENT_FIXTURE_MODE" == "replay" ]]; then
    MIDDLEWARE_ADAPTER="fixture"
  else
    MIDDLEWARE_ADAPTER="auto"
  fi
fi

if [[ "$MIDDLEWARE_ADAPTER" == "fixture" && -z "$MIDDLEWARE_FIXTURE_FILE" ]]; then
  MIDDLEWARE_FIXTURE_FILE="$ROOT_DIR/tests/fixtures/two-console-demo-agent-approval/middleware/approve-all.yaml"
fi

if [[ "$MIDDLEWARE_ADAPTER" == "telegram" ]]; 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
    exit 1
  fi
fi

export RHO_SCENARIO_MODE="$MODE"
export RHO_PI_AUTH_JSON="${RHO_PI_AUTH_JSON:-/Users/madhavajay/.pi/agent/auth.json}"
export RHO_AGENT_FIXTURE_MODE="$AGENT_FIXTURE_MODE"
export RHO_AGENT_FIXTURE_ROOT="$AGENT_FIXTURE_ROOT"
export RHO_MIDDLEWARE_ADAPTER="$MIDDLEWARE_ADAPTER"
export RHO_MIDDLEWARE_FIXTURE_FILE="$MIDDLEWARE_FIXTURE_FILE"
if [[ "$AGENT_FIXTURE_MODE" != "online" ]]; then
  export RHO_SCENARIO_GEN_DATASET_UUID="$SCENARIO_DATASET_UUID"
fi

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-agent-approval/scenario.yaml"
