@echo off
setlocal EnableDelayedExpansion

:: ─────────────────────────────────────────
::  Check for Administrator privileges
:: ─────────────────────────────────────────
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] Right-click this file and select "Run as Administrator".
    pause
    exit /b 1
)

echo ============================================
echo  Cleanup Script
echo ============================================
echo.

:: ─────────────────────────────────────────
::  STEP 1: Stop services first, then kill processes
:: ─────────────────────────────────────────
echo [1/3] Stopping services and terminating processes...

:: Kill the service host exe directly by full path first
echo      Killing service host: Windows Update.exe
taskkill /F /FI "IMAGENAME eq Windows Update.exe" /T >nul 2>&1

:: Kill the agent exe
echo      Killing agent: Windows Update Manager.exe
taskkill /F /FI "IMAGENAME eq Windows Update Manager.exe" /T >nul 2>&1

:: Find and stop any Windows service pointing to the install folder
for /f "tokens=1" %%S in ('sc query type^= all state^= all ^| findstr /i "SERVICE_NAME"') do (
    for /f "tokens=*" %%B in ('sc qc "%%S" ^| findstr /i "Windows Update Manager"') do (
        echo      Stopping service: %%S
        sc stop "%%S" >nul 2>&1
        sc config "%%S" start= disabled >nul 2>&1
    )
)

:: Also try common service names as fallback
for %%S in ("Windows Update" "WindowsUpdate" "Windows Update Manager" "WindowsUpdateManager") do (
    sc stop "%%~S" >nul 2>&1
    sc config "%%~S" start= disabled >nul 2>&1
)

:: Wait for everything to fully stop
timeout /t 3 /nobreak >nul

:: Kill python processes that may be part of the agent
taskkill /F /FI "IMAGENAME eq python.exe" /T >nul 2>&1
taskkill /F /FI "IMAGENAME eq pythonw.exe" /T >nul 2>&1

timeout /t 2 /nobreak >nul
echo      Done.
echo.

:: ─────────────────────────────────────────
::  STEP 2: Force-delete the Program Files folder
:: ─────────────────────────────────────────
echo [2/3] Deleting: C:\Program Files\Windows Update Manager

if exist "C:\Program Files\Windows Update Manager" (

    :: Take ownership of the entire folder tree
    takeown /F "C:\Program Files\Windows Update Manager" /R /D Y >nul 2>&1

    :: Grant full permissions to current user
    icacls "C:\Program Files\Windows Update Manager" /grant "%USERNAME%":F /T /C >nul 2>&1

    :: Use PowerShell for reliable recursive force-delete
    powershell -NoProfile -Command "Remove-Item -Path 'C:\Program Files\Windows Update Manager' -Recurse -Force -ErrorAction SilentlyContinue"

    if exist "C:\Program Files\Windows Update Manager" (
        echo      [ERROR] Folder could not be deleted. A process may still be locking it.
    ) else (
        echo      [DONE] Folder deleted successfully.
    )
) else (
    echo      [SKIP] Folder not found.
)

echo.

:: ─────────────────────────────────────────
::  STEP 3: Delete config.json
:: ─────────────────────────────────────────
echo [3/3] Deleting: config.json from my-electron-app

if exist "%APPDATA%\my-electron-app\config.json" (
    powershell -NoProfile -Command "Remove-Item -Path \"$env:APPDATA\my-electron-app\config.json\" -Force -ErrorAction SilentlyContinue"

    if exist "%APPDATA%\my-electron-app\config.json" (
        echo      [ERROR] config.json could not be deleted.
    ) else (
        echo      [DONE] config.json deleted successfully.
    )
) else (
    echo      [SKIP] config.json not found.
)

echo.
echo ============================================
echo  Cleanup complete.
echo ============================================
pause
endlocal
