@echo off
:: ============================================================
::  DeployPro — Service Manager
::  Manage the DeployPro Windows Service
:: ============================================================

NET SESSION >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
    echo Requesting administrator privileges...
    powershell -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    exit /B
)

cd /d "%~dp0"

:MENU
cls
echo.
echo  ============================================================
echo   DeployPro ^| Service Manager
echo  ============================================================
echo.

:: Show current status inline (service SCM name is deploypro.exe)
FOR /F "tokens=4" %%S IN ('sc query deploypro.exe 2^>nul ^| findstr "STATE"') DO SET SVC_STATE=%%S
IF "%SVC_STATE%"=="" SET SVC_STATE=NOT INSTALLED

echo   Service Status:  %SVC_STATE%
  echo   Web UI:          http://localhost:7070
echo.
echo  ============================================================
echo.
echo   [1]  Start  DeployPro service
echo   [2]  Stop   DeployPro service
echo   [3]  Restart DeployPro service
echo   [4]  Open in browser
echo   [5]  View service status (detailed)
echo   [6]  View service event logs
echo   [I]  Install  DeployPro as Windows service
echo   [U]  Uninstall DeployPro Windows service
echo   [Q]  Quit
echo.
choice /C 123456IUQ /N /M "  Select option: "

IF %ERRORLEVEL% EQU 1 GOTO START_SVC
IF %ERRORLEVEL% EQU 2 GOTO STOP_SVC
IF %ERRORLEVEL% EQU 3 GOTO RESTART_SVC
IF %ERRORLEVEL% EQU 4 GOTO OPEN_BROWSER
IF %ERRORLEVEL% EQU 5 GOTO STATUS
IF %ERRORLEVEL% EQU 6 GOTO EVENTLOG
IF %ERRORLEVEL% EQU 7 GOTO INSTALL
IF %ERRORLEVEL% EQU 8 GOTO UNINSTALL
IF %ERRORLEVEL% EQU 9 GOTO EXIT

:START_SVC
echo.
net start deploypro.exe
timeout /t 2 >nul
GOTO MENU

:STOP_SVC
echo.
net stop deploypro.exe
timeout /t 2 >nul
GOTO MENU

:RESTART_SVC
echo.
net stop deploypro.exe
timeout /t 3 >nul
net start deploypro.exe
timeout /t 2 >nul
GOTO MENU

:OPEN_BROWSER
start "" "http://localhost:7070"
GOTO MENU

:STATUS
echo.
sc query deploypro.exe
echo.
node service\status.js
echo.
pause
GOTO MENU

:EVENTLOG
echo.
echo  Showing last 20 entries from Windows Event Log (Application)...
echo.
powershell -Command "Get-EventLog -LogName Application -Source 'DeployPro wrapper' -Newest 20 -ErrorAction SilentlyContinue | Format-Table TimeGenerated,EntryType,Message -AutoSize -Wrap"
echo.
pause
GOTO MENU

:INSTALL
echo.
echo  ============================================================
echo   DeployPro ^| Install Windows Service
echo  ============================================================
echo.

where node >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
    echo  [ERROR] Node.js not found in PATH. Download from https://nodejs.org/
    echo.
    pause
    GOTO MENU
)

FOR /F "tokens=*" %%V IN ('node --version') DO SET NODE_VER=%%V
echo  Node.js : %NODE_VER%
echo.

IF NOT EXIST "node_modules\node-windows\" (
    echo  Installing dependencies...
    call npm install
    IF %ERRORLEVEL% NEQ 0 (
        echo.
        echo  [ERROR] npm install failed. Check the output above.
        echo.
        pause
        GOTO MENU
    )
    echo  Dependencies OK.
    echo.
)

echo  Registering service...
echo.
node service\install.js
IF %ERRORLEVEL% EQU 0 (
    echo.
    echo  ============================================================
    echo   SUCCESS - DeployPro installed and started.
    echo   Web UI : http://localhost:7070
    echo  ============================================================
) ELSE (
    echo.
    echo  [ERROR] Service installation failed. See output above.
)
echo.
pause
GOTO MENU

:UNINSTALL
echo.
echo  ============================================================
echo   DeployPro ^| Uninstall Windows Service
echo  ============================================================
echo.
node service\uninstall.js
echo.
pause
GOTO MENU

:EXIT
exit /B 0
