#!/usr/bin/env bash
# Status — health check all components

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/core"
source "$SCRIPT_DIR/services"

TALENT_SCOUT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"

check_status() {
  echo -e "${BOLD}Talent Scout — Cluster Status${RESET}"
  echo ""

  echo -e "${BOLD}Infrastructure${RESET}"
  if docker exec "$POSTGRES_CONTAINER" pg_isready -U postgres -q 2>/dev/null; then
    ok "PostgreSQL   :${POSTGRES_PORT}"
  else
    err "PostgreSQL   :${POSTGRES_PORT}  DOWN"
  fi

  if docker exec "$REDIS_CONTAINER" redis-cli ping 2>/dev/null | grep -q PONG; then
    ok "Redis        :${REDIS_PORT}"
  else
    err "Redis        :${REDIS_PORT}  DOWN"
  fi

  echo ""
  echo -e "${BOLD}Services${RESET}"

  if curl -sf "http://localhost:${API_PORT}/api/health" > /dev/null 2>&1; then
    ok "API server   :${API_PORT}"
  else
    warn "API server   :${API_PORT}  not running"
  fi

  if curl -sf "http://localhost:${UI_PORT}" > /dev/null 2>&1; then
    ok "UI (Vite)    :${UI_PORT}"
  else
    warn "UI (Vite)    :${UI_PORT}  not running"
  fi

  if curl -sf "http://localhost:${TOR_PORT}/api/v1/pool/status" > /dev/null 2>&1; then
    ok "Tor manager  :${TOR_PORT}"
  else
    warn "Tor manager  :${TOR_PORT}  not running"
  fi

  services_status

  echo ""
  echo -e "${BOLD}Mail${RESET}"
  if curl -sf "http://localhost:${MAILPIT_PORT}" > /dev/null 2>&1; then
    ok "Mailpit      :${MAILPIT_PORT}"
  else
    warn "Mailpit      :${MAILPIT_PORT}  not running"
  fi

  echo ""
}
