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

# OS guard – must be macOS
if [ "$(uname)" != "Darwin" ]; then
  echo "This command only works on macOS." >&2
  exit 1
fi

# === Parse arguments ===
SKIP_CONFIRM=false
NEW_APP_NAME=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    -y|--yes|--force)
      SKIP_CONFIRM=true
      shift
      ;;
    *)
      NEW_APP_NAME="$1"
      shift
      ;;
  esac
done

if [ -z "$NEW_APP_NAME" ]; then
  echo "Usage: $0 [-y|--yes] \"App Name (e.g. Cursor Enterprise)\"" >&2
  exit 1
fi

APP_PATH="$HOME/Applications/$NEW_APP_NAME.app"
DATA_DIR="$HOME/Library/Application Support/${NEW_APP_NAME// /}"

echo "This will remove:"
echo "  App bundle: $APP_PATH"
echo "  Data dir:   $DATA_DIR"
echo

# Skip confirmation if --yes flag is provided
if [ "$SKIP_CONFIRM" = true ]; then
  ans="y"
else
  read -r -p "Are you sure? [y/N] " ans
fi

case "$ans" in
  y|Y|yes|YES)
    if [ -d "$APP_PATH" ]; then
      echo "🗑 Removing app bundle..."
      rm -rf "$APP_PATH"
    else
      echo "ℹ️ App not found, skipping: $APP_PATH"
    fi

    if [ -d "$DATA_DIR" ]; then
      echo "🗑 Removing data directory..."
      rm -rf "$DATA_DIR"
    else
      echo "ℹ️ Data dir not found, skipping: $DATA_DIR"
    fi

    echo "✅ Cleanup complete."
    ;;
  *)
    echo "Cancelled."
    exit 0
    ;;
esac
