#!/usr/bin/env bash
# ── Live Status Panel ──────────────────────────────────────────────────
# Single-screen health overview. Called by watch every 30s.
# ───────────────────────────────────────────────────────────────────────

B='\033[1m'  ; D='\033[2m'  ; R='\033[0m'
G='\033[32m' ; Y='\033[33m' ; RD='\033[31m' ; C='\033[36m' ; M='\033[35m'
OK="${G}●${R}" ; WARN="${Y}●${R}" ; FAIL="${RD}●${R}" ; OFF="${D}○${R}"

echo -e "${B}${M} ◈ HERMES OPS${R}  ${D}$(date '+%Y-%m-%d %H:%M:%S')${R}"
echo -e "${D}────────────────────────────────────────${R}"

# Gateway
GW_PID=$(pgrep -f "hermes.*gateway.*run" 2>/dev/null | head -1)
if [ -n "$GW_PID" ]; then
    echo -e " ${OK} Gateway        ${D}PID $GW_PID${R}"
else
    echo -e " ${FAIL} Gateway        ${RD}DOWN${R}"
fi

# Kimi loop
KIMI_PID=$(pgrep -f "kimi-loop.sh" 2>/dev/null | head -1)
if [ -n "$KIMI_PID" ]; then
    echo -e " ${OK} Kimi Loop      ${D}PID $KIMI_PID${R}"
else
    echo -e " ${FAIL} Kimi Loop      ${RD}DOWN${R}"
fi

# Active Claude Code (kimi's worker)
CC_PID=$(pgrep -f "claude.*--dangerously" 2>/dev/null | head -1)
if [ -n "$CC_PID" ]; then
    echo -e " ${OK} Claude Code    ${D}PID $CC_PID (working)${R}"
else
    if [ -n "$KIMI_PID" ]; then
        echo -e " ${WARN} Claude Code    ${Y}idle/between issues${R}"
    else
        echo -e " ${OFF} Claude Code    ${D}not running${R}"
    fi
fi

# Gitea VPS
if curl -s --max-time 3 "http://143.198.27.163:3000/api/v1/version" >/dev/null 2>&1; then
    echo -e " ${OK} Gitea VPS      ${D}143.198.27.163:3000${R}"
else
    echo -e " ${FAIL} Gitea VPS      ${RD}UNREACHABLE${R}"
fi

# Dev cycle cron
CRON_LINE=$(hermes cron list 2>&1 | grep -B1 "consolidated-dev-cycle" | head -1 2>/dev/null)
if echo "$CRON_LINE" | grep -q "active"; then
    echo -e " ${OK} Dev Cycle Cron ${D}active (every 30m, ∞)${R}"
elif echo "$CRON_LINE" | grep -q "paused"; then
    echo -e " ${WARN} Dev Cycle Cron ${Y}PAUSED${R}"
else
    echo -e " ${FAIL} Dev Cycle Cron ${RD}MISSING${R}"
fi

echo -e "${D}────────────────────────────────────────${R}"

# Kimi stats
KIMI_LOG="$HOME/.hermes/logs/kimi-loop.log"
if [ -f "$KIMI_LOG" ]; then
    COMPLETED=$(grep -c "SUCCESS:" "$KIMI_LOG" 2>/dev/null || echo 0)
    FAILED=$(grep -c "FAILED:" "$KIMI_LOG" 2>/dev/null || echo 0)
    LAST=$(tail -1 "$KIMI_LOG" 2>/dev/null | cut -d']' -f1 | tr -d '[')
    echo -e " ${B}Kimi Stats${R}"
    echo -e "   Completed: ${G}$COMPLETED${R}  Failed: ${RD}$FAILED${R}"
    echo -e "   Last activity: ${D}$LAST${R}"
    LAST_ISSUE=$(grep "=== ISSUE" "$KIMI_LOG" | tail -1 | sed 's/.*=== ISSUE //' | sed 's/ ===//' 2>/dev/null)
    [ -n "$LAST_ISSUE" ] && echo -e "   Working on: ${C}$LAST_ISSUE${R}"
fi

echo -e "${D}────────────────────────────────────────${R}"

# Process count
HERMES_PROCS=$(ps aux | grep -E "hermes.*python" | grep -v grep | wc -l | tr -d ' ')
PYTEST_PROCS=$(ps aux | grep "pytest" | grep -v grep | wc -l | tr -d ' ')
GIT_STUCK=$(ps aux | grep "git.*push\|git-remote-http" | grep -v grep | wc -l | tr -d ' ')
echo -e " ${B}Processes${R}"
echo -e "   Hermes sessions: $HERMES_PROCS"
[ "$PYTEST_PROCS" -gt 0 ] && echo -e "   Pytest running:  ${Y}$PYTEST_PROCS${R}"
[ "$GIT_STUCK" -gt 0 ] && echo -e "   ${RD}Stuck git:      $GIT_STUCK${R}"

echo -e "${D}────────────────────────────────────────${R}"

# Last audit
LAST_AUDIT=$(ls -t ~/.hermes/audits/audit-*.md 2>/dev/null | head -1)
if [ -n "$LAST_AUDIT" ]; then
    AUDIT_TIME=$(basename "$LAST_AUDIT" | sed 's/audit-//' | sed 's/\.md//' | sed 's/_/ /')
    FINDINGS=$(grep "issue(s) found" "$LAST_AUDIT" 2>/dev/null | head -1)
    echo -e " ${B}Last Audit${R}: ${D}$AUDIT_TIME${R}"
    [ -n "$FINDINGS" ] && echo -e "   $FINDINGS"
fi
