#!/bin/bash
# Fleet Health Check — checks all 8 agents + system resources
# Usage: ./fleet-health-check.sh [--quiet]

QUIET=${1:-""}

declare -A AGENTS=(
  ["rivet"]="18789:rivet-api-2026"
  ["builder"]="18790:builder-api-2026"
  ["susan"]="18792:susan-api-2026"
  ["harper"]="18796:harper-api-2026"
  ["sentinel"]="18800:sentinel-api-2026"
  ["radar"]="18804:radar-api-2026"
  ["herald"]="18808:herald-api-2026"
  ["cog"]="18812:cog-api-2026"
)

FAILURES=0
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S %Z')

echo "=== Fleet Health Check — $TIMESTAMP ==="
echo ""

# Agent checks
echo "--- Agent Status ---"
for agent in rivet builder susan harper sentinel radar herald cog; do
  IFS=':' read -r port token <<< "${AGENTS[$agent]}"
  
  # Check systemd service
  svc="clawdbot-${agent}"
  [ "$agent" = "rivet" ] && svc="clawdbot-gateway"
  
  svc_status=$(systemctl is-active "$svc" 2>/dev/null)
  
  # Check HTTP API
  http_ok=$(curl -s -m 3 -o /dev/null -w "%{http_code}" -X POST "http://127.0.0.1:${port}/tools/invoke" \
    -H "Authorization: Bearer ${token}" \
    -H "Content-Type: application/json" \
    -d '{"tool":"session_status","args":{}}' 2>/dev/null)
  
  # Get memory
  mem=$(systemctl show "$svc" -p MemoryCurrent --value 2>/dev/null)
  mem_mb=$((mem / 1024 / 1024))
  
  if [ "$svc_status" = "active" ] && [ "$http_ok" = "200" ]; then
    echo "  ✅ $agent (port $port) — active, API OK, ${mem_mb}MB RAM"
  else
    echo "  ❌ $agent (port $port) — service=$svc_status, HTTP=$http_ok, ${mem_mb}MB RAM"
    FAILURES=$((FAILURES + 1))
  fi
done

echo ""

# System resources
echo "--- System Resources ---"
CPU_LOAD=$(uptime | awk -F'load average:' '{print $2}' | awk -F, '{print $1}' | tr -d ' ')
MEM_USED=$(free -m | awk '/Mem:/{print $3}')
MEM_TOTAL=$(free -m | awk '/Mem:/{print $2}')
MEM_PCT=$((MEM_USED * 100 / MEM_TOTAL))
DISK_PCT=$(df -h / | awk 'NR==2{print $5}' | tr -d '%')

echo "  CPU Load (1m): $CPU_LOAD"
echo "  RAM: ${MEM_USED}MB / ${MEM_TOTAL}MB (${MEM_PCT}%)"
echo "  Disk: ${DISK_PCT}% used"

# Thresholds
WARNINGS=""
[ "$MEM_PCT" -gt 80 ] && WARNINGS="${WARNINGS}  ⚠️ RAM usage high (${MEM_PCT}%)\n"
[ "$DISK_PCT" -gt 80 ] && WARNINGS="${WARNINGS}  ⚠️ Disk usage high (${DISK_PCT}%)\n"

echo ""

# RateRight app
echo "--- Application ---"
APP_STATUS=$(systemctl is-active rateright-app 2>/dev/null)
APP_HTTP=$(curl -s -m 5 -o /dev/null -w "%{http_code}" https://rivet.rateright.com.au 2>/dev/null)
if [ "$APP_STATUS" = "active" ] && [ "$APP_HTTP" = "200" ]; then
  echo "  ✅ RateRight App — active, HTTPS OK"
else
  echo "  ❌ RateRight App — service=$APP_STATUS, HTTPS=$APP_HTTP"
  FAILURES=$((FAILURES + 1))
fi

echo ""

# Summary
if [ $FAILURES -eq 0 ] && [ -z "$WARNINGS" ]; then
  echo "✅ ALL SYSTEMS GREEN"
else
  [ $FAILURES -gt 0 ] && echo "❌ $FAILURES FAILURES DETECTED"
  [ -n "$WARNINGS" ] && echo -e "$WARNINGS"
fi

exit $FAILURES
