@echo off
setlocal enabledelayedexpansion
title CargoMind - Uninstall Add-in
color 0F

set "CM_DIR=%LOCALAPPDATA%\CargoMind"
set "IDS_FILE=!CM_DIR!\title-ids.txt"
set "FOUND_IDS=0"

echo.
echo  =============================================
echo   CargoMind - Uninstall Add-in
echo  =============================================
echo.

:: Check for any saved installation
set "HAS_IDS=0"
if exist "!IDS_FILE!" set "HAS_IDS=1"
if exist "!CM_DIR!\title-id.txt" set "HAS_IDS=1"

if !HAS_IDS! equ 0 (
    echo  No saved installation found.
    echo  The add-in may not have been installed via this tool.
    echo.
    echo  To remove manually:
    echo  1. Open https://aka.ms/olksideload in your browser
    echo  2. Find CargoMind under "Custom add-ins"
    echo  3. Click the three dots (...) and select Remove
    echo.
    pause
    exit /b 1
)

:: -----------------------------------------------
:: Step 1: Check for Node.js
:: -----------------------------------------------
echo  [1/3] Checking for Node.js...
where node >nul 2>&1
if !errorlevel! neq 0 (
    echo  ERROR: Node.js is required to uninstall.
    echo  Please install from https://nodejs.org and try again.
    echo.
    pause
    exit /b 1
)
echo        Found Node.js.

:: -----------------------------------------------
:: Step 2: Uninstall all accounts via M365 API
:: -----------------------------------------------
echo.
echo  [2/3] Removing add-in from Microsoft 365...

:: Multi-account file
if exist "!IDS_FILE!" (
    for /f "usebackq tokens=*" %%t in ("!IDS_FILE!") do (
        set /a FOUND_IDS+=1
        echo        Removing TitleId: %%t
        call npx -y -p @microsoft/m365agentstoolkit-cli atk uninstall -i false --mode title-id --title-id %%t 2>nul
    )
    del "!IDS_FILE!" 2>nul
)

:: Backward compat: old single title-id.txt
if exist "!CM_DIR!\title-id.txt" (
    set /p TITLE_ID=<"!CM_DIR!\title-id.txt"
    if defined TITLE_ID (
        set /a FOUND_IDS+=1
        echo        Removing TitleId: !TITLE_ID!
        call npx -y -p @microsoft/m365agentstoolkit-cli atk uninstall -i false --mode title-id --title-id !TITLE_ID! 2>nul
    )
    del "!CM_DIR!\title-id.txt" 2>nul
)

echo        Removed !FOUND_IDS! account(s).

:: -----------------------------------------------
:: Step 3: Clean up local data
:: -----------------------------------------------
echo.
echo  [3/3] Cleaning up local data...

:: Close Outlook if running
taskkill /f /im outlook.exe >nul 2>&1
if !errorlevel! equ 0 (
    echo        Outlook closed.
    timeout /t 2 >nul
)

:: Clear WEF cache
if exist "%LOCALAPPDATA%\Microsoft\Office\16.0\Wef" (
    rmdir /s /q "%LOCALAPPDATA%\Microsoft\Office\16.0\Wef" 2>nul
    echo        Outlook cache cleared.
)

echo.
echo  =============================================
echo   Uninstall Complete!
echo  =============================================
echo   Removed from !FOUND_IDS! account(s).
echo   Start Outlook to verify the add-in is gone.
echo  =============================================
echo.

pause
exit /b 0
