#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

echo "==> Online Clinic uninstall (system integration)"
echo
echo "This removes autostart, stops running servers, and optional desktop shortcuts."
echo "Project files in $ROOT_DIR are NOT deleted."
echo

read -r -p "Continue? [y/N]: " confirm
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
  echo "Cancelled."
  exit 0
fi

echo
echo "==> Stopping Laravel API"
bash "$ROOT_DIR/scripts/stop-api.sh" || true

echo "==> Stopping Vite dev server"
bash "$ROOT_DIR/scripts/stop-dev.sh" || true

echo "==> Removing login autostart"
bash "$ROOT_DIR/scripts/autostart/uninstall-autostart.sh" || true

case "$(uname -s)" in
  Darwin)
    SHORTCUT="$HOME/Desktop/Online Clinic.app"
    if [[ -e "$SHORTCUT" ]]; then
      read -r -p 'Remove Desktop shortcut "Online Clinic"? [y/N]: ' remove_shortcut
      if [[ "$remove_shortcut" =~ ^[Yy]$ ]]; then
        rm -rf "$SHORTCUT"
        echo "Removed Desktop shortcut."
      fi
    fi
    ;;
esac

echo
echo "Uninstall complete."
echo "To reinstall: bash scripts/install.sh"
