#!/bin/bash
# VoxDrop - Complete uninstall script
# Removes the application and all its data (config, models, logs, credentials)
#
# Usage:
#   bash /Applications/VoxDrop.app/Contents/Resources/scripts/uninstall.sh
#   -- or --
#   bash uninstall.sh

set -euo pipefail

APP_NAME="VoxDrop"
APP_PATH="/Applications/VoxDrop.app"
CONFIG_DIR="$HOME/.config/voxdrop"
LAUNCH_AGENT="$HOME/Library/LaunchAgents/VoxDrop.plist"
KEYCHAIN_SERVICE="info.korben.voxdrop"
KEYCHAIN_KEY="voxdrop_license_token"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo ""
echo "============================================"
echo "  VoxDrop - Complete Uninstall"
echo "============================================"
echo ""

# Inventory of what will be removed
echo "The following will be removed:"
echo ""

[ -d "$APP_PATH" ] && echo "  [x] Application        $APP_PATH"
[ -d "$CONFIG_DIR" ] && echo "  [x] Configuration      $CONFIG_DIR/config.yaml"
[ -d "$CONFIG_DIR" ] && echo "  [x] History            $CONFIG_DIR/history.json"
[ -d "$CONFIG_DIR" ] && echo "  [x] Substitutions      $CONFIG_DIR/substitutions.txt"
[ -d "$CONFIG_DIR" ] && echo "  [x] Logs               $CONFIG_DIR/voxdrop.log"
[ -d "$CONFIG_DIR/models" ] && {
    MODEL_SIZE=$(du -sh "$CONFIG_DIR/models" 2>/dev/null | cut -f1)
    echo "  [x] AI Models          $CONFIG_DIR/models/ ($MODEL_SIZE)"
}
[ -f "$LAUNCH_AGENT" ] && echo "  [x] Auto-start         $LAUNCH_AGENT"
echo "  [x] Keychain token     $KEYCHAIN_SERVICE/$KEYCHAIN_KEY"
echo "  [x] Permissions        Microphone + Accessibility (TCC reset)"
echo ""

# Confirmation
echo -e "${YELLOW}WARNING: this operation is irreversible.${NC}"
echo -e "${YELLOW}Downloaded models (potentially several GB) will be deleted.${NC}"
echo ""
read -p "Confirm uninstall? (yes/no): " CONFIRM
if [ "$CONFIRM" != "yes" ]; then
    echo "Cancelled."
    exit 0
fi

echo ""

# 1. Quit the app if running
if pgrep -x "voxdrop" > /dev/null 2>&1; then
    echo -n "Closing VoxDrop... "
    killall "voxdrop" 2>/dev/null || true
    sleep 1
    # Force kill if still running
    if pgrep -x "voxdrop" > /dev/null 2>&1; then
        killall -9 "voxdrop" 2>/dev/null || true
    fi
    echo -e "${GREEN}OK${NC}"
fi

# Also kill the translator-worker if running
if pgrep -x "translator-worker" > /dev/null 2>&1; then
    killall "translator-worker" 2>/dev/null || true
fi

# 2. Remove LaunchAgent (auto-start)
if [ -f "$LAUNCH_AGENT" ]; then
    echo -n "Removing auto-start... "
    launchctl unload "$LAUNCH_AGENT" 2>/dev/null || true
    rm -f "$LAUNCH_AGENT"
    echo -e "${GREEN}OK${NC}"
fi

# 3. Remove Keychain token
echo -n "Removing Keychain token... "
security delete-generic-password -s "$KEYCHAIN_SERVICE" -a "$KEYCHAIN_KEY" 2>/dev/null && \
    echo -e "${GREEN}OK${NC}" || echo -e "${YELLOW}(no token found)${NC}"

# 4. Remove configuration directory and models
if [ -d "$CONFIG_DIR" ]; then
    echo -n "Removing configuration and models... "
    rm -rf "$CONFIG_DIR"
    echo -e "${GREEN}OK${NC}"
fi

# 5. Reset TCC permissions (Microphone + Accessibility)
echo -n "Resetting system permissions... "
tccutil reset Microphone info.korben.voxdrop 2>/dev/null || true
tccutil reset Accessibility info.korben.voxdrop 2>/dev/null || true
tccutil reset AppleEvents info.korben.voxdrop 2>/dev/null || true
echo -e "${GREEN}OK${NC}"

# 6. Remove the application
if [ -d "$APP_PATH" ]; then
    echo -n "Removing application... "
    rm -rf "$APP_PATH"
    echo -e "${GREEN}OK${NC}"
fi

echo ""
echo -e "${GREEN}============================================${NC}"
echo -e "${GREEN}  VoxDrop has been completely uninstalled.${NC}"
echo -e "${GREEN}============================================${NC}"
echo ""
echo "To reinstall VoxDrop later, download it"
echo "from your Patreon account."
echo ""
