@echo off
chcp 65001 > nul
title Desinstalador FiscalSinc - Agente FiscalVault

echo ===============================================================================
echo                DESINSTALADOR DO AGENTE FISCALSINC (FISCALVAULT)
echo ===============================================================================
echo.
echo  Este utilitario ira remover o agente FiscalSinc do seu computador.
echo.

set "INSTALL_DIR=C:\FiscalSinc"

set /p CONFIRM="Tem certeza que deseja desinstalar o FiscalSinc? [S/N] (Padrao: N): "
if /i not "%CONFIRM%"=="S" (
    echo Operacao cancelada pelo usuario.
    pause
    exit /b 0
)

echo.
echo [1/4] Encerrando processos do FiscalSinc...
taskkill /f /im FiscalVaultSyncGUI.exe >nul 2>&1
taskkill /f /im FiscalVaultSync.exe >nul 2>&1

echo [2/4] Removendo atalhos do Desktop e Inicializacao...
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
    "$desktop = [Environment]::GetFolderPath('Desktop'); " ^
    "$startup = [Environment]::GetFolderPath('Startup'); " ^
    "Remove-Item -Path \"$desktop\FiscalSinc - Agente FiscalVault.lnk\" -Force -ErrorAction SilentlyContinue; " ^
    "Remove-Item -Path \"$startup\FiscalSinc.lnk\" -Force -ErrorAction SilentlyContinue;"

echo [3/4] Removendo tarefas agendadas caso existam...
schtasks /delete /tn "FiscalSinc_SyncAgent" /f >nul 2>&1

echo [4/4] Removendo arquivos do programa...
set /p REMOVE_DATA="Deseja excluir tambem o historico e configuracoes (sync_state.db / sync_config.json)? [S/N] (Padrao: N): "
if /i "%REMOVE_DATA%"=="S" (
    if exist "%INSTALL_DIR%" rmdir /s /q "%INSTALL_DIR%"
    echo Diretorio de instalacao completamente removido.
) else (
    if exist "%INSTALL_DIR%\FiscalVaultSyncGUI.exe" del /f /q "%INSTALL_DIR%\FiscalVaultSyncGUI.exe"
    if exist "%INSTALL_DIR%\FiscalVaultSync.exe" del /f /q "%INSTALL_DIR%\FiscalVaultSync.exe"
    echo Executaveis removidos. Configuracoes e historico foram preservados em %INSTALL_DIR%.
)

echo.
echo ===============================================================================
echo  Desinstalacao concluida com sucesso!
echo ===============================================================================
echo.
pause
exit /b 0
