@echo off
title Uninstall OmidPaymentPCPOS Service

:: ============================
:: Elevate to Administrator
:: ============================
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo Requesting administrative privileges...
    powershell -Command "Start-Process '%~f0' -Verb runAs"
    exit /b
)

:: ============================
:: Variables
:: ============================
set "CURRENT_DIR=%~dp0"
:: Remove trailing backslash if present
if "%CURRENT_DIR:~-1%"=="\" set "CURRENT_DIR=%CURRENT_DIR:~0,-1%"

set "APP_DIR=%CURRENT_DIR%\OmidPayPCPOSService"

:: Detect OS architecture
if defined ProgramFiles(x86) (
    set "ARCH=64"
) else (
    set "ARCH=32"
)

if "%ARCH%"=="64" (
    set "NSSM_PATH=%APP_DIR%\nssm\win64\nssm.exe"
) else (
    set "NSSM_PATH=%APP_DIR%\nssm\win32\nssm.exe"
)

set "SERVICE_NAME=OmidPaymentPCPOS"
set "LOG_DIR=%APP_DIR%\logs"

echo.
echo ===========================================
echo   Uninstalling Service: %SERVICE_NAME%
echo ===========================================
echo Using NSSM: %NSSM_PATH%
echo.

:: ============================
:: Stop the service
:: ============================
echo Stopping service if running...
sc stop %SERVICE_NAME% >nul 2>&1
timeout /t 2 >nul

:: ============================
:: Remove the service
:: ============================
echo Removing service...
"%NSSM_PATH%" remove %SERVICE_NAME% confirm
if %errorlevel% neq 0 (
    echo [WARNING] Service may not have existed or removal failed.
) else (
    echo Service removed successfully.
)

:: ============================
:: Optional: delete logs
:: ============================
if exist "%LOG_DIR%" (
    echo Deleting logs directory...
    rmdir /s /q "%LOG_DIR%"
)

echo.
echo [SUCCESS] Uninstallation process completed.
echo Press any key to exit...
pause >nul
