#!/usr/bin/env bash
# Server-side component checks on koopa (via SSH).
set -euo pipefail
ROOT=$(cd "$(dirname "$0")" && pwd)
# shellcheck source=lib.sh
source "$ROOT/lib.sh"

# Area server-### — host/container ports via SSH
set_area server
section "server · ssh ${KOOPA_SSH}"

if ! koopa_ssh_ok; then
  fail "ssh ${KOOPA_SSH}" "unreachable within ${SSH_CONNECT_TIMEOUT}s (SKIP_SSH=1 to skip)"
  summary
  exit 1
fi
ok "ssh ${KOOPA_SSH}"

# Run a remote script; collect structured lines
REMOTE=$(koopa_ssh_bash "${SSH_CMD_TIMEOUT}" <<'REMOTE'
set +e
report() { printf 'R|%s|%s|%s\n' "$1" "$2" "$3"; }

# containers
for name in taler-hacktivism-bank taler-hacktivism-exchange-ansible taler-hacktivism; do
  if podman ps --format '{{.Names}}' 2>/dev/null | grep -qx "$name"; then
    st=$(podman ps --filter "name=^${name}$" --format '{{.Status}}' | head -1)
    report OK "container $name" "$st"
  else
    # soft match
    hit=$(podman ps --format '{{.Names}}' 2>/dev/null | grep -E "$name|bank|exchange|hacktivism" | head -3 | tr '\n' ' ')
    if podman ps --format '{{.Names}}' 2>/dev/null | grep -q bank && [ "$name" = taler-hacktivism-bank ]; then
      bn=$(podman ps --format '{{.Names}}' | grep -i bank | head -1)
      report OK "container bank ($bn)" "$(podman ps --filter name="$bn" --format '{{.Status}}' | head -1)"
    elif podman ps --format '{{.Names}}' 2>/dev/null | grep -qi exchange && echo "$name" | grep -qi exchange; then
      en=$(podman ps --format '{{.Names}}' | grep -i exchange | head -1)
      report OK "container exchange ($en)" "$(podman ps --filter name="$en" --format '{{.Status}}' | head -1)"
    elif podman ps --format '{{.Names}}' 2>/dev/null | grep -qx taler-hacktivism && [ "$name" = taler-hacktivism ]; then
      report OK "container taler-hacktivism" "$(podman inspect -f '{{.State.Status}}' taler-hacktivism 2>/dev/null)"
    else
      report FAIL "container $name" "not running (seen: $hit)"
    fi
  fi
done

# local HTTP on pasta ports
for spec in \
  "bank-api|http://127.0.0.1:9012/config|200" \
  "bank-integration|http://127.0.0.1:9012/taler-integration/config|200" \
  "landing|http://127.0.0.1:9013/intro/|200" \
  "exchange|http://127.0.0.1:9011/config|200" \
  "merchant|https://127.0.0.1:9010/config|200"
do
  IFS='|' read -r id url want <<<"$spec"
  code=$(curl -skS -m 5 -o /tmp/mon-s.out -w '%{http_code}' "$url" 2>/dev/null || echo 000)
  if [ "$code" = "$want" ]; then
    report OK "local $id : ${url#*//}" "HTTP $code"
  else
    report FAIL "local $id" "HTTP $code want $want"
  fi
done

# processes inside bank
BANK=$(podman ps --format '{{.Names}}' | grep -iE 'bank|hacktivism-bank' | head -1)
if [ -n "$BANK" ]; then
  if podman exec "$BANK" bash -c 'pgrep -f "MainKt serve|libeufin-bank serve" >/dev/null' 2>/dev/null; then
    report OK "libeufin-bank process" "in $BANK"
  else
    report FAIL "libeufin-bank process" "not running in $BANK"
  fi
  if podman exec "$BANK" bash -c 'pg_isready -q' 2>/dev/null; then
    report OK "postgres (bank)" "ready"
  else
    report WARN "postgres (bank)" "pg_isready failed"
  fi
  if podman exec "$BANK" bash -c 'pgrep -x nginx >/dev/null' 2>/dev/null; then
    report OK "nginx landing" "in $BANK"
  else
    report WARN "nginx landing" "not seen in $BANK"
  fi
else
  report FAIL "bank container" "none"
fi

# exchange process / systemd if any
EX=$(podman ps --format '{{.Names}}' | grep -i exchange | head -1)
if [ -n "$EX" ]; then
  if podman exec "$EX" bash -c 'pgrep -f taler-exchange-httpd >/dev/null || systemctl is-active taler-exchange-httpd 2>/dev/null | grep -q active' 2>/dev/null; then
    report OK "exchange-httpd" "in $EX"
  else
    # config answering is enough
    report WARN "exchange-httpd process" "not detected; port check above"
  fi
fi

MER=$(podman ps --format '{{.Names}}' | grep -E '^taler-hacktivism$' | head -1)
[ -z "$MER" ] && MER=$(podman ps --format '{{.Names}}' | grep -i merchant | head -1)
if [ -n "$MER" ]; then
  if podman exec "$MER" bash -c 'pgrep -f taler-merchant-httpd >/dev/null || true; curl -sk -m 3 -o /dev/null -w %{http_code} https://127.0.0.1:9010/config' 2>/dev/null | grep -q 200; then
    report OK "merchant-httpd" "responds in $MER"
  else
    report WARN "merchant-httpd" "check manually in $MER"
  fi
fi

# caddy on host
if systemctl is-active caddy >/dev/null 2>&1 || pgrep -x caddy >/dev/null 2>&1; then
  report OK "caddy" "active"
else
  report WARN "caddy" "not detected as active"
fi
REMOTE
)

while IFS= read -r line; do
  case "$line" in
    R\|*)
      IFS='|' read -r _ st label detail <<<"$line"
      case "$st" in
        OK) ok "$label${detail:+ ($detail)}" ;;
        FAIL) fail "$label" "$detail" ;;
        WARN) warn "$label" "$detail" ;;
      esac
      ;;
  esac
done <<<"$REMOTE"

summary
