#!/usr/bin/env bash
# Peppa dev stand: isolated DB (~/.peppa-dev/peppa.db), tmux server (-L peppa-dev), state dir
# (~/.peppa-dev), the Peppa bot against mock-telegram, mock agents by default.
# Lets an agent play the PM in Telegram without a human and without touching prod.
#
#   peppa-dev up | down | status
#   peppa-dev peppa <args>          Peppa CLI against the stand (give projects to the test PM:
#                                   peppa-dev peppa project owner <KEY> mock-pm)
#   peppa-dev tg messages|click|reply|send|callbacks ...
set -euo pipefail
ROOT="$(cd "$(dirname "$(readlink -f "$0")")/../../.." && pwd)"
PORT="${PEPPA_DEV_TG_PORT:-3190}"
export PEPPA_ROOT="$ROOT"
export PEPPA_HOME="$HOME/.peppa-dev"
export PEPPA_DB="$PEPPA_HOME/peppa.db"
export PEPPA_WORKSPACES="$HOME/.peppa-dev/workspaces"
export PEPPA_TMUX_SOCKET="peppa-dev"
export TELEGRAM_BOT_TOKEN="mock-123:TOKEN"
export TELEGRAM_API_URL="http://127.0.0.1:$PORT"
export PEPPA_CLAUDE_BIN="${PEPPA_DEV_CLAUDE_BIN:-$ROOT/packages/testkit/bin/mock-claude}"
export PEPPA_CODEX_BIN="${PEPPA_DEV_CODEX_BIN:-$ROOT/packages/testkit/bin/mock-codex}"
export PEPPA_HOOK_WAIT_SEC="${PEPPA_HOOK_WAIT_SEC:-30}"
export PEPPA_WEB_URL="http://peppa-dev.local"
# PEP-208: the stand talks to mock-telegram — no night silence there.
export PEPPA_NIGHT_QUIET="${PEPPA_NIGHT_QUIET:-off}"
# A worktree has no .env: the stand still needs a key for its own encrypted columns.
[ -n "${PEPPA_SECRET:-}" ] || grep -q '^PEPPA_SECRET=' "$ROOT/.env" 2>/dev/null || export PEPPA_SECRET="mock-dev-stand-secret"
mkdir -p "$PEPPA_HOME" "$PEPPA_WORKSPACES"
TM=(tmux -L peppa-dev)
PEPPA="$ROOT/packages/cli/bin/peppa"

up() {
  "$PEPPA" db migrate >/dev/null
  "${TM[@]}" has-session -t dev-mock-tg 2>/dev/null || "${TM[@]}" new-session -d -s dev-mock-tg "$ROOT/packages/testkit/bin/mock-telegram --port $PORT"
  "${TM[@]}" set-option -s exit-empty off
  for _ in $(seq 1 40); do curl -sf "$TELEGRAM_API_URL/_mock/messages" >/dev/null && break; sleep 0.25; done
  # PEP-21: the watchdog is the runner service — the bot hands it answers and «В работу».
  "${TM[@]}" has-session -t dev-watchdog 2>/dev/null || "${TM[@]}" new-session -d -s dev-watchdog "$PEPPA watchdog"
  for _ in $(seq 1 40); do [ -S "$PEPPA_HOME/runner.sock" ] && break; sleep 0.25; done
  "${TM[@]}" has-session -t dev-bot 2>/dev/null || "${TM[@]}" new-session -d -s dev-bot "$ROOT/apps/bot/bin/peppa-bot"
  for _ in $(seq 1 40); do "$PEPPA" telegram status 2>/dev/null | grep -q '^bot: @' && break; sleep 0.25; done
  "$PEPPA" user ensure mock-pm >/dev/null
  if ! "$PEPPA" telegram status | grep -q '^mock-pm: chat'; then
    code=$("$PEPPA" telegram pair mock-pm | grep -o 'start=[A-Za-z0-9_-]*' | cut -d= -f2)
    curl -sf -X POST "$TELEGRAM_API_URL/_mock/send" -H 'Content-Type: application/json' -d "{\"text\":\"/start $code\"}" >/dev/null
    for _ in $(seq 1 40); do "$PEPPA" telegram status | grep -q '^mock-pm: chat' && break; sleep 0.25; done
  fi
  status
}

status() {
  echo "processes:"; "${TM[@]}" ls -F '  #{session_name}' 2>/dev/null || echo "  (stand is down)"
  "$PEPPA" telegram status 2>/dev/null | sed 's/^/  /' || true
}

case "${1:-}" in
  up) up ;;
  down) "${TM[@]}" kill-server 2>/dev/null || true; echo "stand is down" ;;
  status) status ;;
  peppa) shift; exec "$PEPPA" "$@" ;;
  tg) shift; exec "${PEPPA_NODE:-node}" --import "$ROOT/scripts/lib/require-node.mjs" "$ROOT/packages/testkit/src/peppa-dev-tg.ts" "$@" ;;
  *) sed -n '2,9p' "$0" | sed 's/^# \{0,1\}//'; exit 1 ;;
esac
