#!/usr/bin/env bash
# ==============================================================================
# NEXUS Core Daemon & System Inspector CLI
# Version: 4.2.0-enterprise
# ==============================================================================
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export NEXUS_HOME="$DIR"

case "$1" in
    status)
        echo "=== NEXUS NODE STATUS ==="
        python3 "$DIR/system_inspector.py" --once
        ;;
    start)
        echo "[+] Starting Nexus Background Daemon..."
        nohup python3 "$DIR/nexus_daemon.py" > "$DIR/nexus.log" 2>&1 &
        echo $! > "$DIR/nexus.pid"
        echo "[OK] Daemon started (PID $(cat "$DIR/nexus.pid")). Logs: $DIR/nexus.log"
        ;;
    stop)
        if [ -f "$DIR/nexus.pid" ]; then
            PID=$(cat "$DIR/nexus.pid")
            kill -15 "$PID" 2>/dev/null || true
            rm -f "$DIR/nexus.pid"
            echo "[OK] Nexus daemon stopped."
        else
            echo "[!] No active daemon PID found."
        fi
        ;;
    inspector)
        python3 "$DIR/system_inspector.py" "${@:2}"
        ;;
    test-alert)
        python3 "$DIR/system_inspector.py" --test-alert
        ;;
    heal)
        python3 "$DIR/system_inspector.py" --heal "${2:-CACHE}"
        ;;
    *)
        echo "NEXUS OS Command Line Interface v4.2.0"
        echo "Usage: nexus {status|start|stop|inspector|test-alert|heal}"
        exit 1
        ;;
esac
