#!/usr/bin/env bash
# ── Consolidated Ops Panel ─────────────────────────────────────────────
# Everything in one view. Designed for a half-screen pane (~100x45).
# ───────────────────────────────────────────────────────────────────────

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

TOKEN=$(cat ~/.hermes/gitea_token_vps 2>/dev/null)
API="http://143.198.27.163:3000/api/v1/repos/rockachopa/Timmy-time-dashboard"

# ── HEADER ─────────────────────────────────────────────────────────────
echo ""
echo -e "  ${B}${M}◈ HERMES OPERATIONS${R}                         ${D}$(date '+%a %b %d  %H:%M:%S')${R}"
echo -e "  ${D}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${R}"
echo ""

# ── SERVICES ───────────────────────────────────────────────────────────
echo -e "  ${B}${U}SERVICES${R}"
echo ""

# Gateway
GW_PID=$(pgrep -f "hermes.*gateway.*run" 2>/dev/null | head -1)
[ -n "$GW_PID" ] && echo -e "    ${OK} Gateway          ${D}pid $GW_PID${R}" \
                  || echo -e "    ${FAIL} Gateway          ${RD}DOWN — run: hermes gateway start${R}"

# Kimi Code loop
KIMI_PID=$(pgrep -f "kimi-loop.sh" 2>/dev/null | head -1)
[ -n "$KIMI_PID" ] && echo -e "    ${OK} Kimi Loop        ${D}pid $KIMI_PID${R}" \
                    || echo -e "    ${FAIL} Kimi Loop        ${RD}DOWN — run: ops-wake-kimi${R}"

# Active Kimi Code worker
KIMI_WORK=$(pgrep -f "kimi.*--print" 2>/dev/null | head -1)
if [ -n "$KIMI_WORK" ]; then
    echo -e "    ${OK} Kimi Code        ${D}pid $KIMI_WORK ${G}working${R}"
elif [ -n "$KIMI_PID" ]; then
    echo -e "    ${WARN} Kimi Code        ${Y}between issues${R}"
else
    echo -e "    ${OFF} Kimi Code        ${D}not running${R}"
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

# Matrix staging
HTTP=$(curl -s --max-time 3 -o /dev/null -w "%{http_code}" "http://143.198.27.163/")
[ "$HTTP" = "200" ] && echo -e "    ${OK} Matrix Staging   ${D}143.198.27.163${R}" \
                     || echo -e "    ${FAIL} Matrix Staging   ${RD}HTTP $HTTP${R}"

# 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
    NEXT=$(hermes cron list 2>&1 | grep -A4 "consolidated-dev-cycle" | grep "Next" | awk '{print $NF}' | cut -dT -f2 | cut -d. -f1)
    echo -e "    ${OK} Dev Cycle        ${D}every 30m, next ${NEXT:-?}${R}"
else
    echo -e "    ${FAIL} Dev Cycle Cron   ${RD}MISSING${R}"
fi

echo ""

# ── KIMI STATS ─────────────────────────────────────────────────────────
echo -e "  ${B}${U}KIMI${R}"
echo ""
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_ISSUE=$(grep "=== ISSUE" "$KIMI_LOG" | tail -1 | sed 's/.*=== //' | sed 's/ ===//')
    LAST_TIME=$(grep "=== ISSUE\|SUCCESS\|FAILED" "$KIMI_LOG" | tail -1 | cut -d']' -f1 | tr -d '[')
    RATE=""
    if [ "$COMPLETED" -gt 0 ] && [ "$FAILED" -gt 0 ]; then
        TOTAL=$((COMPLETED + FAILED))
        PCT=$((COMPLETED * 100 / TOTAL))
        RATE=" (${PCT}% success)"
    fi
    echo -e "    Completed  ${G}${B}$COMPLETED${R}    Failed  ${RD}$FAILED${R}${D}$RATE${R}"
    echo -e "    Current    ${C}$LAST_ISSUE${R}"
    echo -e "    Last seen  ${D}$LAST_TIME${R}"
fi
echo ""

# ── OPEN PRS ───────────────────────────────────────────────────────────
echo -e "  ${B}${U}PULL REQUESTS${R}"
echo ""
curl -s --max-time 5 -H "Authorization: token $TOKEN" "$API/pulls?state=open&limit=8" 2>/dev/null | python3 -c "
import json,sys
try:
    prs = json.loads(sys.stdin.read())
    if not prs: print('    \033[2m(none open)\033[0m')
    for p in prs[:6]:
        n = p['number']
        t = p['title'][:55]
        u = p['user']['login']
        print(f'    \033[33m#{n:<4d}\033[0m \033[2m{u:8s}\033[0m {t}')
    if len(prs) > 6: print(f'    \033[2m... +{len(prs)-6} more\033[0m')
except: print('    \033[31m(error fetching)\033[0m')
" 2>/dev/null
echo ""

# ── RECENTLY MERGED ────────────────────────────────────────────────────
echo -e "  ${B}${U}RECENTLY MERGED${R}"
echo ""
curl -s --max-time 5 -H "Authorization: token $TOKEN" "$API/pulls?state=closed&sort=updated&limit=5" 2>/dev/null | python3 -c "
import json,sys
try:
    prs = json.loads(sys.stdin.read())
    merged = [p for p in prs if p.get('merged')][:5]
    if not merged: print('    \033[2m(none recent)\033[0m')
    for p in merged:
        n = p['number']
        t = p['title'][:50]
        when = p['merged_at'][11:16]
        print(f'    \033[32m✓ #{n:<4d}\033[0m {t}  \033[2m{when}\033[0m')
except: print('    \033[31m(error)\033[0m')
" 2>/dev/null
echo ""

# ── KIMI QUEUE ─────────────────────────────────────────────────────────
echo -e "  ${B}${U}KIMI QUEUE${R}"
echo ""
curl -s --max-time 5 -H "Authorization: token $TOKEN" "$API/issues?state=open&assignee=kimi&limit=10&type=issues" 2>/dev/null | python3 -c "
import json,sys
try:
    issues = json.loads(sys.stdin.read())
    if not issues: print('    \033[33m⚠ Queue empty — assign more issues to kimi\033[0m')
    for i in issues[:6]:
        n = i['number']
        t = i['title'][:55]
        print(f'    #{n:<4d} {t}')
    if len(issues) > 6: print(f'    \033[2m... +{len(issues)-6} more\033[0m')
except: print('    \033[31m(error)\033[0m')
" 2>/dev/null
echo ""

# ── WARNINGS ───────────────────────────────────────────────────────────
HERMES_PROCS=$(ps aux | grep -E "hermes.*python" | grep -v grep | wc -l | tr -d ' ')
STUCK_GIT=$(ps aux | grep "git.*push\|git-remote-http" | grep -v grep | wc -l | tr -d ' ')
ORPHAN_PY=$(ps aux | grep "pytest tests/" | grep -v grep | wc -l | tr -d ' ')
UNASSIGNED=$(curl -s --max-time 3 -H "Authorization: token $TOKEN" "$API/issues?state=open&limit=50&type=issues" 2>/dev/null | python3 -c "import json,sys; issues=json.loads(sys.stdin.read()); print(len([i for i in issues if not i.get('assignees')]))" 2>/dev/null)

WARNS=""
[ "$STUCK_GIT" -gt 0 ] && WARNS+="    ${RD}⚠ $STUCK_GIT stuck git processes${R}\n"
[ "$ORPHAN_PY" -gt 0 ] && WARNS+="    ${Y}⚠ $ORPHAN_PY orphaned pytest runs${R}\n"
[ "${UNASSIGNED:-0}" -gt 10 ] && WARNS+="    ${Y}⚠ $UNASSIGNED unassigned issues — feed the queue${R}\n"

if [ -n "$WARNS" ]; then
    echo -e "  ${B}${U}WARNINGS${R}"
    echo ""
    echo -e "$WARNS"
fi

echo -e "  ${D}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${R}"
echo -e "  ${D}hermes sessions: $HERMES_PROCS    unassigned: ${UNASSIGNED:-?}    ↻ 20s${R}"
