#!/bin/bash
# ComputeHive Node Uninstaller (macOS). Double-click to run.
echo "============================================"
echo "  ComputeHive Node - Uninstaller"
echo "============================================"
echo ""
echo "Stopping the node and its background service..."

# Remove this node from the dashboard using the saved config (token + broker).
CFG="$HOME/.computehive/node.json"
if [ -f "$CFG" ]; then
  TOKEN=$(python3 -c "import json;print(json.load(open('$CFG')).get('token',''))" 2>/dev/null)
  BROKER=$(python3 -c "import json;print(json.load(open('$CFG')).get('brokerUrl',''))" 2>/dev/null)
  if [ -n "$TOKEN" ] && [ -n "$BROKER" ]; then
    curl -s -m 8 -X POST "$BROKER/api/nodes/deregister" -H "Content-Type: application/json" -d "{\"daemonToken\":\"$TOKEN\"}" >/dev/null 2>&1
  fi
fi

# Remove and stop the launch agent
launchctl unload "$HOME/Library/LaunchAgents/io.computehive.node.plist" 2>/dev/null
rm -f "$HOME/Library/LaunchAgents/io.computehive.node.plist"

# Stop the daemon and the image server
pkill -f "computehive/daemon.js" 2>/dev/null
pkill -f "imggen-server.py" 2>/dev/null

echo "Removing ComputeHive files and downloaded models..."
echo "(Ollama and Python are left installed - remove them yourself if unused.)"
rm -f "$HOME/Library/Logs/computehive-node.log"
rm -rf "$HOME/.computehive"
rm -rf "$HOME/computehive"

echo ""
echo "ComputeHive node has been removed. You can close this window."
echo ""
read -n 1 -s -r -p "Press any key to close..."
echo ""
