#!/usr/bin/env bash
# AI Log Triage & Diagnostics v2.5.0 (Zero-Config / High-Precision)

set -euo pipefail

# --- Configuration & State Paths ---
CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/py-agent"
CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/py-agent"
CURSOR_FILE="${CACHE_DIR}/log-checker.cursor"

MYSYS_FILE="${CONFIG_DIR}/skills/system/mysys.md"
[[ -f "$MYSYS_FILE" ]] || MYSYS_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/local-ai/skills/system/mysys.md"

mkdir -p "$CACHE_DIR"

# --- CLI Options ---
MODE="incremental"
while [[ $# -gt 0 ]]; do
    case "$1" in
        --all|-a)
            MODE="all"
            shift
            ;;
        --kernel|-k)
            MODE="kernel"
            shift
            ;;
        --reset|-r)
            rm -f "$CURSOR_FILE"
            echo ":: Cursor reset. Next run will inspect the last 30 minutes."
            exit 0
            ;;
        --help|-h)
            echo "Usage: log-checker [--all|-a] [--kernel|-k] [--reset|-r]"
            echo "  --all     Inspect entire current boot (bypasses cursor)"
            echo "  --kernel  Show high-priority kernel and driver messages only"
            echo "  --reset   Reset cursor bookmark"
            exit 0
            ;;
        *)
            shift
            ;;
    esac
done

echo "# ACTIVE SYSTEM DIAGNOSTICS & LOG TRIAGE"
echo ""
echo "> **Directives for AI:**"
echo "> 1. Analyze high-severity events first: failed units, kernel/DRM/GPU panics, and coredumps."
echo "> 2. Filter out desktop background noise (e.g. GTK CSS warnings, xdg-desktop-portal idle inhibitions, or tracker-miner notices)."
echo "> 3. If 'NO NEW EVENTS RECORDED' is reported, confirm cleanly that the system is quiet and previous errors are mitigated."
echo "> 4. Identify the root cause in 2 sentences max, followed by an exact, verified recovery action or terminal command."
echo "> 5. Use semantic, compact Markdown styled for terminal output."
echo ""
echo "---"
echo ""

# ==========================================
# SECTION 1: SYSTEMD FAILED UNITS
# ==========================================
if command -v systemctl >/dev/null 2>&1; then
    sys_failed=$(systemctl --failed --no-legend --no-pager 2>/dev/null || true)
    usr_failed=$(systemctl --user --failed --no-legend --no-pager 2>/dev/null || true)

    if [[ -n "$sys_failed" || -n "$usr_failed" ]]; then
        echo "## FAILED SYSTEMD SERVICES"
        echo '```text'
        [[ -n "$sys_failed" ]] && echo "[System Units]:" && echo "$sys_failed"
        [[ -n "$usr_failed" ]] && echo "[User Units]:" && echo "$usr_failed"
        echo '```'
        echo ""
        echo "---"
        echo ""
    fi
fi

# ==========================================
# SECTION 2: RECENT COREDUMPS (SIGSEGV / SIGABRT)
# ==========================================
if command -v coredumpctl >/dev/null 2>&1; then
    recent_dumps=$(coredumpctl list --no-legend --no-pager -n 3 2>/dev/null || true)
    if [[ -n "$recent_dumps" ]]; then
        echo "## RECENT PROCESS CRASHES & COREDUMPS"
        echo '```text'
        echo "$recent_dumps"
        echo '```'
        echo ""
        echo "---"
        echo ""
    fi
fi

# ==========================================
# SECTION 3: KERNEL & DRIVER ERRORS (Hardware / DRM / GPU / OOM)
# ==========================================
if command -v journalctl >/dev/null 2>&1; then
    kerrors=$(journalctl -k -b 0 -p err..emerg --no-pager -n 25 2>/dev/null || true)
    if [[ -n "$kerrors" ]]; then
        echo "## KERNEL & DRIVER ALERTS (Ring 0 / GPU / Memory)"
        echo '```text'
        echo "$kerrors"
        echo '```'
        echo ""
        echo "---"
        echo ""
    fi
fi

# ==========================================
# SECTION 4: DYNAMIC SYSTEM EVENTS (Incremental / Windowed)
# ==========================================
if command -v journalctl >/dev/null 2>&1; then
    new_logs=""

    if [[ "$MODE" == "kernel" ]]; then
        echo "## KERNEL EVENTS (Current Boot - Last 50)"
        echo '```text'
        journalctl -k -b 0 -n 50 --no-pager 2>/dev/null || true
        echo '```'
    elif [[ "$MODE" == "all" ]]; then
        echo "## FULL BOOT EVENTS (Errors & Warnings - Last 60)"
        echo '```text'
        journalctl -b 0 -p warning..emerg -n 60 --no-pager 2>/dev/null || true
        echo '```'
    else
        # Incremental cursor-tracked mode
        if [[ ! -f "$CURSOR_FILE" ]]; then
            new_logs=$(journalctl -b 0 --since "30 minutes ago" --cursor-file="$CURSOR_FILE" -n 50 2>/dev/null || true)
        else
            new_logs=$(journalctl -b 0 --cursor-file="$CURSOR_FILE" -n 50 2>/dev/null || true)
        fi

        if [[ -n "$new_logs" ]]; then
            echo "## NEW SYSTEM EVENTS"
            echo '```text'
            echo "$new_logs"
            echo '```'
        else
            echo "## NO NEW EVENTS RECORDED (System is Quiet)"
            echo "No new system errors or warnings recorded since the last check."
            echo ""
            echo "### LAST RECORDED CONTEXT (Tail 5)"
            echo '```text'
            journalctl -b 0 -n 5 --no-pager 2>/dev/null || true
            echo '```'
        fi
    fi
else
    echo "## DIAGNOSTIC ERROR"
    echo "*Error: journalctl utility not found on this system.*"
fi

echo ""
echo "---"
echo ""

# ==========================================
# SECTION 5: PRIVILEGE AUDIT & SYSTEM PROFILE
# ==========================================
if command -v journalctl >/dev/null 2>&1; then
    if ! journalctl -b 0 -n 1 >/dev/null 2>&1; then
        echo "## PRIVILEGE RESTRICTION NOTICE"
        echo "⚠️ **Notice:** User cannot read system-level journal logs (restricted to user-space)."
        echo "To view full hardware & daemon journals, add user to group: \`sudo usermod -aG systemd-journal $USER\`"
        echo ""
        echo "---"
        echo ""
    fi
fi

if [[ -f "$MYSYS_FILE" ]]; then
    echo "## SYSTEM ARCHITECTURE PROFILE"
    cat "$MYSYS_FILE"
    echo ""
fi
