#!/bin/bash
# ============================================================
# KreateFlo — macOS Uninstaller
# Safely removes ONLY KreateFlo files. Nothing else is touched.
# ============================================================

clear
echo ""
echo "  ╔══════════════════════════════════════════╗"
echo "  ║       KreateFlo — macOS Uninstaller      ║"
echo "  ╚══════════════════════════════════════════╝"
echo ""

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

# ---- Safety: make sure HOME is set and sane ----
if [ -z "$HOME" ] || [ "$HOME" = "/" ]; then
    echo "${RED}Error: HOME is not set correctly. Aborting for safety.${NC}"
    echo ""
    read -n 1 -s -r -p "Press any key to close..."
    exit 1
fi

# ---- User-level paths (no password needed) ----
USER_TARGETS=(
    "$HOME/Library/Application Support/Adobe/CEP/extensions/com.kreateflo.editor"
    "$HOME/Library/Application Support/KreateFlo"
    "$HOME/Applications/KreateFlo Spotlight.app"
    "$HOME/Library/LaunchAgents/com.kreateflo.spotlight.plist"
    "$HOME/Library/Preferences/com.kreateflo.editor.plist"
    "$HOME/Library/Caches/com.kreateflo.editor"
)

# ---- System-level path (installed by the .pkg, needs admin password) ----
SYSTEM_TARGETS=(
    "/Library/Application Support/Adobe/CEP/extensions/com.kreateflo.editor"
)

echo "This will remove KreateFlo and its downloaded AI tools:"
echo ""
found_any=0
for t in "${USER_TARGETS[@]}" "${SYSTEM_TARGETS[@]}"; do
    if [ -e "$t" ]; then
        echo "  • $t"
        found_any=1
    fi
done
if [ "$found_any" -eq 0 ]; then
    echo "  (nothing found — KreateFlo may already be uninstalled)"
fi
echo ""
echo "${YELLOW}Your projects, sequences and media are NOT touched.${NC}"
echo ""
read -r -p "Type 'yes' to uninstall: " CONFIRM

if [ "$CONFIRM" != "yes" ]; then
    echo ""
    echo "Cancelled. Nothing was removed."
    echo ""
    read -n 1 -s -r -p "Press any key to close..."
    exit 0
fi

echo ""
removed=0

# ---- Stop the Spotlight companion (best-effort) ----
osascript -e 'tell application "KreateFlo Spotlight" to quit' >/dev/null 2>&1 || true
pkill -f "KreateFlo Spotlight" >/dev/null 2>&1 || true
launchctl unload "$HOME/Library/LaunchAgents/com.kreateflo.spotlight.plist" >/dev/null 2>&1 || true
launchctl remove com.kreateflo.spotlight >/dev/null 2>&1 || true

# ---- Remove user-level paths ----
for t in "${USER_TARGETS[@]}"; do
    # Extra guard: each target must live inside the user's home folder
    case "$t" in
        "$HOME/"*)
            if [ -e "$t" ]; then
                rm -rf "$t" && echo "${GREEN}✓ Removed:${NC} $t" && removed=$((removed+1))
            fi
            ;;
        *)
            echo "${RED}Skipped (unexpected path):${NC} $t"
            ;;
    esac
done

# ---- Remove system-level paths (admin password) ----
need_sudo=0
for t in "${SYSTEM_TARGETS[@]}"; do
    [ -e "$t" ] && need_sudo=1
done

if [ "$need_sudo" -eq 1 ]; then
    echo ""
    echo "${YELLOW}The Premiere extension was installed system-wide.${NC}"
    echo "${YELLOW}Your Mac password is needed to remove it (it is never stored).${NC}"
    echo ""
    for t in "${SYSTEM_TARGETS[@]}"; do
        # Extra guard: only the exact KreateFlo CEP extension path
        case "$t" in
            "/Library/Application Support/Adobe/CEP/extensions/com.kreateflo.editor")
                if [ -e "$t" ]; then
                    sudo rm -rf "$t" && echo "${GREEN}✓ Removed:${NC} $t" && removed=$((removed+1))
                fi
                ;;
            *)
                echo "${RED}Skipped (unexpected path):${NC} $t"
                ;;
        esac
    done
fi

echo ""
if [ "$removed" -gt 0 ]; then
    echo "${GREEN}KreateFlo has been uninstalled ($removed item(s) removed).${NC}"
else
    echo "Nothing found to remove — KreateFlo may already be uninstalled."
fi
echo ""
echo "Tip: restart Premiere Pro to finish."
echo ""
read -n 1 -s -r -p "Press any key to close..."
echo ""
