@echo off
title OmidPaymentPCPOS Service Installer

:: ============================
:: Prompt for installation
:: ============================
echo ================================================
echo OmidPaymentPCPOS Service Installation
echo ================================================
echo.
set /p USER_CHOICE="[?] Install the OmidPaymentPCPOS Service? (Y/N): "
if /I not "%USER_CHOICE%"=="Y" (
    echo Installation cancelled by user.
    pause
    exit /b 0
)

:: ============================
:: 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%"

:: Paths
set "APP_DIR=%CURRENT_DIR%\OmidPayPCPOSService"
set "APP_PATH=%APP_DIR%\OmidPaymentPCPOS.exe"

:: 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"
    set "ASPNET_RUNTIME=%APP_DIR%\aspnetcore-runtime-8.0.20-win-x64.exe"
    set "DOTNET_RUNTIME=%APP_DIR%\dotnet-runtime-8.0.20-win-x64.exe"
) else (
    set "NSSM_PATH=%APP_DIR%\nssm\win32\nssm.exe"
    set "ASPNET_RUNTIME=%APP_DIR%\aspnetcore-runtime-8.0.20-win-x86.exe"
    set "DOTNET_RUNTIME=%APP_DIR%\dotnet-runtime-8.0.20-win-x86.exe"
)

set "SERVICE_NAME=OmidPaymentPCPOS"

echo Detected OS architecture: %ARCH%-bit
echo Using NSSM path: %NSSM_PATH%
echo Service executable: %APP_PATH%
echo ASP .Net RunTime: %ASPNET_RUNTIME%
echo Dot .Net RunTime: %DOTNET_RUNTIME%
echo.

:: ============================
:: Install .NET Runtimes if needed
:: ============================
echo [INFO] Installing required runtimes...

if exist "%DOTNET_RUNTIME%" (
    echo Installing .NET Runtime...
    "%DOTNET_RUNTIME%" /install /quiet /norestart
) else (
    echo [ERROR] .NET Runtime installer not found: %DOTNET_RUNTIME%
    pause
    exit /b 1
)

if exist "%ASPNET_RUNTIME%" (
    echo Installing ASP.NET Core Runtime...
    "%ASPNET_RUNTIME%" /install /quiet /norestart
) else (
    echo [ERROR] ASP.NET Core Runtime installer not found: %ASPNET_RUNTIME%
    pause
    exit /b 1
)

echo.
:: ============================
:: Install the service
:: ============================
echo Installing service %SERVICE_NAME% ...
"%NSSM_PATH%" install %SERVICE_NAME% "%APP_PATH%"
if %errorlevel% neq 0 (
    echo [ERROR] Failed to install service.
    pause
    exit /b 1
)

:: ============================
:: Set working directory
:: ============================
"%NSSM_PATH%" set %SERVICE_NAME% AppDirectory "%APP_DIR%"

:: ============================
:: Set startup type to automatic
:: ============================
sc config %SERVICE_NAME% start= auto

:: ============================
:: Optional restart behavior
:: ============================
REM "%NSSM_PATH%" set %SERVICE_NAME% AppExit Default Restart
REM "%NSSM_PATH%" set %SERVICE_NAME% AppThrottle 5000

:: ============================
:: Start the service
:: ============================
echo Starting service %SERVICE_NAME% ...
"%NSSM_PATH%" start %SERVICE_NAME%

echo.
echo [SUCCESS] Service %SERVICE_NAME% installed and running.
pause
