@echo off
setlocal

set "ROOT_DIR=%~dp0.."
set "CONFIG_FILE=%USERPROFILE%\Saleculator.properties"

echo.
echo Saleculator First Run Setup
echo ===========================
echo.
echo This assistant prepares Saleculator for trial or live use.
echo Use option 1 for a single POS or the main POS/server in a network.
echo Use option 2 for extra POS machines that connect to an existing database.
echo.
echo Requirements:
echo - Java 8 or newer
echo - MySQL Server and MySQL client tools for standalone/main POS
echo - MySQL client tools for network clients
echo.
echo Note: this assistant configures Saleculator and MySQL access. It does not
echo silently install MySQL Server; install MySQL first for standalone/main POS.
echo.

:menu
echo Select setup action:
echo   1. Standalone or main POS database on this machine
echo   2. Network client connecting to an existing POS database
echo   3. Test current database connection
echo   4. Run preflight check
echo   5. Launch Saleculator
echo   6. Collect support info
echo   7. Exit
echo.
set /p "CHOICE=Enter 1-7: "

if "%CHOICE%"=="1" goto local_db
if "%CHOICE%"=="2" goto network_db
if "%CHOICE%"=="3" goto test_db
if "%CHOICE%"=="4" goto preflight
if "%CHOICE%"=="5" goto launch
if "%CHOICE%"=="6" goto support
if "%CHOICE%"=="7" goto done

echo Invalid choice.
echo.
goto menu

:local_db
call "%ROOT_DIR%\scripts\setup-local-db.bat" "%CONFIG_FILE%"
if errorlevel 1 goto failed
call "%ROOT_DIR%\scripts\preflight.bat" "%CONFIG_FILE%"
echo.
echo Next: start Saleculator and approve database creation if prompted.
echo For a 30 day trial, use the START TRIAL button on the activation screen,
echo then paste the trial serial key into Saleculator and activate it.
echo.
goto launch_prompt

:network_db
call "%ROOT_DIR%\scripts\setup-network-client.bat"
if errorlevel 1 goto failed
call "%ROOT_DIR%\scripts\preflight.bat"
echo.
echo Next: start Saleculator and login with the shop's assigned user/PIN.
echo For a 30 day trial, use the START TRIAL button on the activation screen,
echo then paste the trial serial key into Saleculator and activate it.
echo.
goto launch_prompt

:test_db
powershell -NoProfile -ExecutionPolicy Bypass -File "%ROOT_DIR%\scripts\configure-db.ps1" -Mode test
if errorlevel 1 goto failed
echo.
goto menu

:preflight
call "%ROOT_DIR%\scripts\preflight.bat"
echo.
goto menu

:launch
call "%ROOT_DIR%\scripts\run.bat"
goto done

:support
call "%ROOT_DIR%\scripts\support-info.bat"
echo.
goto menu

:launch_prompt
set /p "OPEN_APP=Launch Saleculator now? [Y/n]: "
if /i "%OPEN_APP%"=="N" goto done
call "%ROOT_DIR%\scripts\run.bat"
goto done

:failed
echo.
echo Setup did not complete.
echo Run scripts\support-info.bat and share the generated support-info file
echo with your Saleculator support contact if you need help.
exit /b 1

:done
exit /b 0
