#!/usr/bin/env bash

# Nomarchy Debug Information Script
# Collects system information for troubleshooting.

set -e

# Flags for agent use to avoid hanging
PRINT_ONLY=false
if [[ "$*" == *"--print"* ]]; then
    PRINT_ONLY=true
fi

echo "--- Nomarchy Debug Info ---"
echo "Version: $(nomarchy-version)"
echo "Host: $(hostname)"
echo "Kernel: $(uname -r)"
echo "Uptime: $(uptime -p)"

echo ""
echo "--- Graphics ---"
if command -v hyprctl &>/dev/null; then
    hyprctl version | head -n 1
fi
if command -v glxinfo &>/dev/null; then
    glxinfo | grep "OpenGL renderer" || echo "OpenGL: info not available"
fi

echo ""
echo "--- Nix/NixOS ---"
nix --version
if [[ -f /etc/os-release ]]; then
    grep "PRETTY_NAME" /etc/os-release | cut -d'"' -f2
fi

echo ""
echo "--- Nomarchy State ---"
STATE_FILE="$HOME/.config/nomarchy/state.json"
if [[ -f "$STATE_FILE" ]]; then
    jq '.' "$STATE_FILE"
else
    echo "State file not found at $STATE_FILE"
fi

echo ""
echo "--- Services Status ---"
for svc in waybar hypridle hyprlock walker fprintd fwupd; do
    if systemctl is-active --quiet "$svc" 2>/dev/null || systemctl --user is-active --quiet "$svc" 2>/dev/null; then
        echo "[ACTIVE] $svc"
    else
        echo "[INACTIVE] $svc"
    fi
done

echo ""
echo "--- End of Debug Info ---"

if [[ "$PRINT_ONLY" == "false" ]]; then
    echo ""
    echo "Would you like to upload this log to a pastebin? (y/N)"
    read -r answer
    if [[ "$answer" =~ ^[Yy]$ ]]; then
        if command -v nomarchy-upload-log &>/dev/null; then
            nomarchy-debug --print | nomarchy-upload-log
        else
            echo "Error: nomarchy-upload-log not found."
        fi
    fi
fi
