#!/usr/bin/env bash
# Full AIOR CONNECT removal — all versions, agent, configs, apps, pkg receipts, downloads.
# Run before a clean v0.5.0+ install. Safe to re-run.
set -euo pipefail

HOME_DIR="${HOME}"
USER_UID="$(id -u)"
LABEL="gui/${USER_UID}/com.aior.connect.agent"
REMOVED=()
SKIP_SUDO=false

for arg in "$@"; do
  case "$arg" in
    --no-sudo) SKIP_SUDO=true ;;
    -h|--help)
      cat <<'HELP'
Usage: clean-mac-aiorconnect-full.sh [--no-sudo]

Removes:
  - LaunchAgent com.aior.connect.agent
  - ~/.aiorconnect (all versions, copied agents, logs)
  - AIOR CONNECT.app in /Applications and ~/Applications
  - Legacy /usr/local/bin/aiorconnect-agent and aiorconnect-desktop
  - Library Preferences / Caches / Saved State / Application Support
  - Old installers in ~/Downloads (AIOR CONNECT*.dmg/pkg/zip only)
  - pkg receipts com.aior.connect (for clean .pkg reinstall)

After run: System Settings → Privacy → Screen Recording / Accessibility
  → remove old AICN-service entries manually, then install fresh .pkg.

Install fresh: https://aiorconnect.com/get-started
HELP
      exit 0
      ;;
  esac
done

note() { echo "==> $*"; }
did_remove() { REMOVED+=("$1"); }

note "Stopping AIOR CONNECT processes and LaunchAgent"
launchctl bootout "$LABEL" 2>/dev/null || true
launchctl disable "$LABEL" 2>/dev/null || true
pkill -f "AICN-service" 2>/dev/null || true
pkill -f "AIORCONNECT" 2>/dev/null || true
pkill -f "aiorconnect-agent" 2>/dev/null || true
pkill -f "AIOR CONNECT" 2>/dev/null || true

PLIST="${HOME_DIR}/Library/LaunchAgents/com.aior.connect.agent.plist"
if [[ -f "$PLIST" ]]; then
  rm -f "$PLIST"
  did_remove "LaunchAgent plist"
fi

remove_path() {
  local p="$1"
  if [[ -e "$p" ]]; then
    rm -rf "$p" 2>/dev/null && did_remove "$p" && return 0
    return 1
  fi
  return 0
}

remove_with_admin() {
  local p="$1"
  [[ -e "$p" ]] || return 0
  if $SKIP_SUDO; then
    echo "WARN: need admin to remove $p (re-run without --no-sudo)"
    return 0
  fi
  local q
  q="$(printf '%s' "$p" | sed 's/"/\\"/g')"
  if osascript -e "do shell script \"rm -rf \\\"$q\\\"\" with administrator privileges" 2>/dev/null; then
    did_remove "$p (admin)"
  else
    echo "WARN: could not remove $p — delete manually or run with admin"
  fi
}

note "Removing config and data"
for p in \
  "${HOME_DIR}/.aiorconnect" \
  "${HOME_DIR}/Library/Saved Application State/com.aior.connect.savedState" \
  "${HOME_DIR}/Library/Saved Application State/com.aior.connect.agent.savedState" \
  "${HOME_DIR}/Library/Preferences/com.aior.connect.plist" \
  "${HOME_DIR}/Library/Preferences/com.aior.connect.agent.plist" \
  "${HOME_DIR}/Library/Caches/com.aior.connect" \
  "${HOME_DIR}/Library/Caches/com.aior.connect.agent" \
  "${HOME_DIR}/Library/Application Support/com.aior.connect" \
  "${HOME_DIR}/Library/Application Support/AIOR CONNECT" \
  "${HOME_DIR}/Library/Logs/AIOR CONNECT" \
  "${HOME_DIR}/Library/Logs/AICN-service" \
  "${HOME_DIR}/Library/WebKit/com.aior.connect" \
  "${HOME_DIR}/Library/Containers/com.aior.connect" \
  "${HOME_DIR}/Library/Group Containers/"*".com.aior.connect" \
  ; do
  remove_path "$p" 2>/dev/null || true
done

note "Removing legacy CLI binaries"
for p in /usr/local/bin/aiorconnect-agent /usr/local/bin/aiorconnect-desktop /usr/local/bin/AICN-service; do
  if [[ -e "$p" ]]; then
    if $SKIP_SUDO; then
      echo "WARN: legacy $p — remove with: sudo rm -f $p"
    else
      osascript -e "do shell script \"rm -f '$p'\" with administrator privileges" 2>/dev/null && did_remove "$p" || true
    fi
  fi
done

note "Removing application bundles"
for app in \
  "/Applications/AIOR CONNECT.app" \
  "/Applications/AIOR Connect.app" \
  "${HOME_DIR}/Applications/AIOR CONNECT.app" \
  "${HOME_DIR}/Applications/AIOR Connect.app" \
  ; do
  if [[ -d "$app" ]]; then
    if [[ -w "$app" ]] || [[ -w "$(dirname "$app")" ]]; then
      remove_path "$app" && continue
    fi
    remove_with_admin "$app"
  fi
done

note "Removing old installers from Downloads"
shopt -s nullglob
for f in \
  "${HOME_DIR}/Downloads/AIOR CONNECT"*.dmg \
  "${HOME_DIR}/Downloads/AIOR CONNECT"*.pkg \
  "${HOME_DIR}/Downloads/AIOR CONNECT"*.zip \
  "${HOME_DIR}/Downloads/AIOR CONNECT"*.app.zip \
  "${HOME_DIR}/Downloads/AIOR-CONNECT"*.dmg \
  "${HOME_DIR}/Downloads/AIOR-CONNECT"*.pkg \
  "${HOME_DIR}/Downloads/AIOR-CONNECT"*.exe \
  ; do
  rm -f "$f" 2>/dev/null && did_remove "$f"
done
shopt -u nullglob

note "Forgetting pkg receipts (clean reinstall)"
for pkg in com.aior.connect com.aior.connect.agent; do
  if pkgutil --pkgs 2>/dev/null | grep -qx "$pkg"; then
    if $SKIP_SUDO; then
      echo "WARN: run: sudo pkgutil --forget $pkg"
    else
      osascript -e "do shell script \"pkgutil --forget $pkg\" with administrator privileges" 2>/dev/null && did_remove "pkg:$pkg" || true
    fi
  fi
done

note "Resetting TCC cache hints (optional — may prompt)"
if command -v tccutil >/dev/null 2>&1; then
  tccutil reset ScreenCapture com.aior.connect 2>/dev/null || true
  tccutil reset ScreenCapture com.aior.connect.agent 2>/dev/null || true
  tccutil reset Accessibility com.aior.connect 2>/dev/null || true
  tccutil reset Accessibility com.aior.connect.agent 2>/dev/null || true
fi

echo ""
echo "CLEAN_MAC_AIORCONNECT_OK removed=${#REMOVED[@]}"
if [[ ${#REMOVED[@]} -gt 0 ]]; then
  printf '  - %s\n' "${REMOVED[@]}"
fi
echo ""
echo "Manual (recommended before fresh install):"
echo "  System Settings → Privacy & Security → Screen Recording"
echo "    → remove old AICN-service / aiorconnect-agent entries"
echo "  System Settings → Privacy & Security → Accessibility"
echo "    → remove old AIOR CONNECT / AICN-service entries"
echo ""
echo "Fresh install: https://aiorconnect.com/get-started → AIOR CONNECT.pkg (v0.5.0+)"
