#!/bin/bash
# aria-status — zeigt Gesundheit der Aria-Installation
set -u

GREEN='\033[0;32m'; RED='\033[0;31m'; YELLOW='\033[1;33m'
CYAN='\033[0;36m'; BOLD='\033[1m'; NC='\033[0m'

ok()   { echo -e "  ${GREEN}✓${NC} $*"; }
bad()  { echo -e "  ${RED}✗${NC} $*"; }
warn() { echo -e "  ${YELLOW}⚠${NC} $*"; }

echo ""
echo -e "  ${BOLD}${CYAN}Aria Status${NC}"
echo -e "  ────────────────────────────"

# Services
for s in aria aria-chat-logger aria-health; do
    if systemctl is-active "${s}.service" >/dev/null 2>&1; then
        ok "${s}.service aktiv"
    else
        bad "${s}.service inaktiv"
    fi
done

# Health-Endpoint
if curl -fsS --max-time 3 http://127.0.0.1:9090/health >/dev/null 2>&1; then
    ok "Health-Endpoint (localhost:9090) antwortet"
    STATUS=$(curl -fsS http://127.0.0.1:9090/health 2>/dev/null | python3 -c "import json,sys; d=json.load(sys.stdin); print(d.get('status','?'))")
    echo -e "     → Gesamt-Status: ${BOLD}${STATUS}${NC}"
else
    bad "Health-Endpoint nicht erreichbar"
fi

# Telegram
TG_ENV="$HOME/.claude/channels/telegram/.env"
if [ -f "$TG_ENV" ]; then
    TG_TOKEN=$(grep '^TELEGRAM_BOT_TOKEN=' "$TG_ENV" | cut -d= -f2-)
    if [ -n "$TG_TOKEN" ]; then
        if curl -fsS --max-time 5 "https://api.telegram.org/bot${TG_TOKEN}/getMe" | grep -q '"ok":true'; then
            ok "Telegram-Bot erreichbar"
        else
            bad "Telegram-Bot antwortet nicht (Token falsch?)"
        fi
    else
        warn "Telegram-Token leer"
    fi
else
    warn "Telegram nicht konfiguriert"
fi

# Letzte Chat-Message
DB="$HOME/.aria-chat.db"
if [ -f "$DB" ]; then
    LAST=$(sqlite3 "$DB" "SELECT created_at FROM aria_chat_log ORDER BY created_at DESC LIMIT 1;" 2>/dev/null)
    if [ -n "$LAST" ]; then
        ok "Letzte Chat-Message: $LAST"
    else
        warn "Chat-Log leer (noch keine Nachricht)"
    fi
else
    warn "Chat-DB existiert nicht ($DB)"
fi

# Anthropic Key (wenn API-Mode)
ENV_F="$HOME/.env"
if [ -f "$ENV_F" ] && grep -q '^ANTHROPIC_API_KEY=' "$ENV_F"; then
    KEY=$(grep '^ANTHROPIC_API_KEY=' "$ENV_F" | cut -d= -f2-)
    CODE=$(curl -s --max-time 8 -o /dev/null -w "%{http_code}" \
        -X POST https://api.anthropic.com/v1/messages \
        -H "x-api-key: $KEY" -H "anthropic-version: 2023-06-01" \
        -H "content-type: application/json" \
        -d '{"model":"claude-haiku-4-5-20251001","max_tokens":1,"messages":[{"role":"user","content":"hi"}]}')
    if [ "$CODE" = "200" ]; then
        ok "Anthropic API-Key gueltig"
    elif [ "$CODE" = "401" ] || [ "$CODE" = "403" ]; then
        bad "Anthropic API-Key ungueltig (HTTP $CODE)"
    else
        warn "Anthropic API antwortet mit HTTP $CODE"
    fi
else
    echo -e "  ${CYAN}ℹ${NC} Kein API-Key (Max/Pro-Plan) — nicht pruefbar"
fi

# Brain-Sync
BRAIN="$HOME/aria/brain"
if [ -d "$BRAIN/.git" ]; then
    LAST_COMMIT=$(cd "$BRAIN" && git log -1 --format='%ci %s' 2>/dev/null)
    [ -n "$LAST_COMMIT" ] && ok "Brain letzter Commit: $LAST_COMMIT" || warn "Brain ohne Commits"
else
    warn "Brain ohne Git-Sync"
fi

echo ""
echo -e "  ${CYAN}Details:${NC} curl -s http://127.0.0.1:9090/health | jq"
echo -e "  ${CYAN}Logs:${NC}    journalctl -u aria -f"
echo ""
