#!/usr/bin/env bash
# uninstall.sh — Remove all Sysadmin Chronicles game-owned host resources.
#
# Removes:
#   - sc- prefixed libvirt VM domains (after confirmation)
#   - sc- prefixed libvirt networks
#   - sc-images storage pool and its directory
#   - SSH key pair (~/.ssh/sc_host_key)
#
# Does NOT remove:
#   - The game directory itself (remove manually if desired)
#   - Any system-wide libvirt config
#   - Any resources not prefixed with sc-
#
# Usage:
#   bash tools/setup/uninstall.sh
#   bash tools/setup/uninstall.sh --dry-run
#   bash tools/setup/uninstall.sh --yes  (skip confirmation)

set -euo pipefail

DRY_RUN=false
ASSUME_YES=false

for arg in "$@"; do
    case "$arg" in
        --dry-run) DRY_RUN=true ;;
        --yes)     ASSUME_YES=true ;;
    esac
done

run() {
    if [ "$DRY_RUN" = "true" ]; then echo "  [DRY-RUN] $*"; else "$@"; fi
}

echo ""
echo "══════════════════════════════════════════════════"
echo "  Sysadmin Chronicles — Uninstall"
echo "══════════════════════════════════════════════════"
[ "$DRY_RUN" = "true" ] && echo "  [DRY-RUN mode — no changes]"
echo ""
echo "  This will PERMANENTLY remove all sc- prefixed VMs,"
echo "  networks, storage, and SSH keys."
echo ""

if [ "$ASSUME_YES" = "false" ] && [ "$DRY_RUN" = "false" ]; then
    read -rp "  Type YES to confirm uninstall: " confirm
    if [ "$confirm" != "YES" ]; then
        echo "  Aborted."
        exit 0
    fi
fi

# ---------------------------------------------------------------------------
# Remove VMs
# ---------------------------------------------------------------------------
echo ""
echo "── Removing VM domains ──────────────────────────"
for domain in $(virsh list --all --name 2>/dev/null | grep "^sc-" || true); do
    echo "  Removing $domain..."
    # Stop if running
    if virsh domstate "$domain" 2>/dev/null | grep -q "running"; then
        run virsh destroy "$domain"
    fi
    # Remove all snapshots
    for snap in $(virsh snapshot-list "$domain" --name 2>/dev/null || true); do
        run virsh snapshot-delete "$domain" "$snap"
    done
    run virsh undefine "$domain" --remove-all-storage
    echo "  ✓ $domain removed"
done

# ---------------------------------------------------------------------------
# Remove networks
# ---------------------------------------------------------------------------
echo ""
echo "── Removing sc- networks ─────────────────────────"
for net in $(virsh net-list --all --name 2>/dev/null | grep "^sc-" || true); do
    echo "  Removing network $net..."
    if virsh net-info "$net" 2>/dev/null | grep -q "Active:.*yes"; then
        run virsh net-destroy "$net"
    fi
    run virsh net-undefine "$net"
    echo "  ✓ $net removed"
done

# ---------------------------------------------------------------------------
# Remove storage pool
# ---------------------------------------------------------------------------
echo ""
echo "── Removing sc-images storage pool ──────────────"
if virsh pool-list --all | grep -q "sc-images"; then
    POOL_PATH=$(virsh pool-dumpxml sc-images 2>/dev/null | grep -oP '(?<=<path>)[^<]+' || echo "")
    if virsh pool-info sc-images 2>/dev/null | grep -q "State:.*running"; then
        run virsh pool-destroy sc-images
    fi
    run virsh pool-undefine sc-images
    if [ -n "$POOL_PATH" ] && [ -d "$POOL_PATH" ]; then
        run rm -rf "$POOL_PATH"
    fi
    echo "  ✓ sc-images pool removed"
else
    echo "  (sc-images pool not found — skipping)"
fi

# ---------------------------------------------------------------------------
# Remove SSH keys
# ---------------------------------------------------------------------------
echo ""
echo "── Removing SSH keys ─────────────────────────────"
KEY="$HOME/.ssh/sc_host_key"
if [ -f "$KEY" ]; then
    run rm -f "$KEY" "${KEY}.pub"
    echo "  ✓ SSH keys removed"
else
    echo "  (No sc_host_key found — skipping)"
fi

echo ""
echo "══════════════════════════════════════════════════"
echo "  Uninstall complete."
echo "  Game files (this directory) were not removed."
echo "══════════════════════════════════════════════════"
echo ""
