#!/usr/bin/env bash
# =============================================================================
# status.sh — which mode is this box in, and is TrakopLens whole?
#
#     run/status.sh
#
# Needs no root. Answers the two questions you actually have before touching
# anything: what is serving the site right now, and can the assistant still
# retrieve. It PROBES rather than reading configuration, because "configured"
# and "working" have diverged on this box before.
# =============================================================================
set -uo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.." || exit 1
REPO_ROOT="$(pwd)"

G=$'\033[32m'; R=$'\033[31m'; Y=$'\033[33m'; B=$'\033[1m'; N=$'\033[0m'

printf "\n${B}=== RUNTIME ===${N}\n"

CONTAINERS="$(docker ps --format '{{.Names}}' 2>/dev/null | grep -c '^trakop-' || true)"
APACHE="$(systemctl is-active apache2 2>/dev/null)"
CODE80="$(curl -s -o /dev/null -w '%{http_code}' -m 8 http://localhost/trakop-web/code/ 2>/dev/null)"

# The container stamps a header on its responses; host Apache does not. That is the
# only reliable way to tell them apart, since both answer on :80 and both are Apache.
if curl -sI -m 6 http://localhost/trakop-web/code/ 2>/dev/null | grep -qi 'X-Trakop-Runtime'; then
    SERVED_BY="Docker container"
else
    SERVED_BY="host Apache + php7.4-fpm"
fi

if [ "$CONTAINERS" -gt 0 ]; then
    printf "  mode          ${B}DOCKER${N}  (%s trakop containers up)\n" "$CONTAINERS"
else
    printf "  mode          ${B}WITHOUT DOCKER${N}  (no trakop containers)\n"
fi
printf "  http://localhost/trakop-web/code/   HTTP %s   served by %s\n" "$CODE80" "$SERVED_BY"
printf "  host apache2  %s\n" "$APACHE"

# :8400 is realtime driver tracking. In Docker mode the container provides it; without
# Docker the trakop-node systemd unit does, and it is NOT installed by default.
NODE="$(curl -s -o /dev/null -w '%{http_code}' -m 5 http://127.0.0.1:8400/ 2>/dev/null)"
if [ "$NODE" = "000" ]; then
    printf "  :8400 node    ${Y}down${N}  (realtime driver tracking unavailable)\n"
else
    printf "  :8400 node    ${G}up${N} (HTTP %s)\n" "$NODE"
fi

printf "\n${B}=== TRAKOPLENS RETRIEVAL ===${N}\n"
STATUS="$(php "$REPO_ROOT/code/tools/trakoplens/host_status.php" 2>/dev/null)"
BACKEND="$(printf '%s\n' "$STATUS" | sed -n 's/^resolved_backend=//p')"
READY="$(printf '%s\n' "$STATUS"  | grep -c '=READY' || true)"
case "$BACKEND" in
    pgvector)      printf "  answering via ${G}pgvector${N} — AI embeddings (the container stack)\n" ;;
    mysql-vector)  printf "  answering via ${G}mysql-vector${N} — AI embeddings, index in MySQL\n" ;;
    mysql-lexical) printf "  answering via ${G}mysql-lexical${N} — word matching, no servers needed\n" ;;
    *)             printf "  ${R}no backend answered${N} — retrieval is DEAD, see below\n" ;;
esac
printf "  %s of 3 backends indexed and ready\n" "$READY"
printf '%s\n' "$STATUS" | sed -n 's/^backend\./    backend /p'

printf "\n${B}=== SWITCH ===${N}\n"
printf "  run/with-docker.sh       start the containers, they take over :80\n"
printf "  run/without-docker.sh    stop the containers, host Apache takes :80\n"
printf "  (add --dry-run to either to preview without changing anything)\n\n"
