@echo off
REM ============================================================
REM  Zorro's Print Agent - Windows Auto-Start Installer
REM
REM  Run this from the same folder as zorros_print_agent.py on the
REM  Windows computer that stays on at the store. It installs a
REM  Scheduled Task so the kitchen print spooler starts when the
REM  user logs in and restarts if the Python process exits.
REM ============================================================

setlocal EnableExtensions

echo.
echo   ZORRO'S PRINT AGENT - WINDOWS AUTO-START
echo   This installs the kitchen print spooler as a login task.
echo.

set "APP_DIR=%LOCALAPPDATA%\ZorrosPrintAgent"
set "ENV_FILE=%APP_DIR%\print-agent.env.cmd"
set "RUNNER=%APP_DIR%\run_print_agent.bat"
set "TASK_NAME=ZorrosPrintAgent"
set "SOURCE_AGENT=%~dp0zorros_print_agent.py"

if not exist "%SOURCE_AGENT%" (
    echo ERROR: zorros_print_agent.py must be in the same folder as this installer.
    pause
    exit /b 1
)

python --version >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
    echo ERROR: Python was not found.
    echo Install Python from https://python.org/downloads, then run this again.
    pause
    exit /b 1
)

mkdir "%APP_DIR%" >nul 2>&1
copy /Y "%SOURCE_AGENT%" "%APP_DIR%\zorros_print_agent.py" >nul

echo Installing Python dependency: requests
python -m pip install requests --quiet --disable-pip-version-check

echo.
echo Checking phone-order readiness before setup...
python "%SOURCE_AGENT%" --status
echo.

if "%PRINT_BRIDGE_TOKEN%"=="" (
    set /p PRINT_BRIDGE_TOKEN=Enter the print bridge token from Veli:
)
if "%PRINT_BRIDGE_TOKEN%"=="" (
    echo ERROR: PRINT_BRIDGE_TOKEN is required.
    pause
    exit /b 1
)

if "%PRINTER_IP%"=="" set "PRINTER_IP=192.168.213.109"
if "%PRINTER_PORT%"=="" set "PRINTER_PORT=9100"

set /p INPUT_PRINTER_IP=Printer IP [%PRINTER_IP%]:
if not "%INPUT_PRINTER_IP%"=="" set "PRINTER_IP=%INPUT_PRINTER_IP%"

set /p INPUT_PRINTER_PORT=Printer port [%PRINTER_PORT%]:
if not "%INPUT_PRINTER_PORT%"=="" set "PRINTER_PORT=%INPUT_PRINTER_PORT%"

(
    echo set "RAILWAY_URL=%RAILWAY_URL%"
    echo if "%%RAILWAY_URL%%"=="" set "RAILWAY_URL=https://zorro-phone-agent-production.up.railway.app"
    echo set "PRINT_BRIDGE_TOKEN=%PRINT_BRIDGE_TOKEN%"
    echo set "PRINTER_IP=%PRINTER_IP%"
    echo set "PRINTER_PORT=%PRINTER_PORT%"
    echo if "%%PRINT_POLL_INTERVAL%%"=="" set "PRINT_POLL_INTERVAL=10"
) > "%ENV_FILE%"

(
    echo @echo off
    echo setlocal EnableExtensions
    echo call "%ENV_FILE%"
    echo cd /d "%APP_DIR%"
    echo :restart
    echo python "%APP_DIR%\zorros_print_agent.py"
    echo echo Print agent stopped. Restarting in 10 seconds...
    echo timeout /t 10 /nobreak ^>nul
    echo goto restart
) > "%RUNNER%"

echo.
echo Testing Railway token plus printer path with one setup ticket...
call "%ENV_FILE%"
python "%APP_DIR%\zorros_print_agent.py" --test-bridge-print
if %ERRORLEVEL% NEQ 0 (
    echo.
    echo WARNING: Setup ticket test failed.
    echo The task will still install, but check Wi-Fi, printer IP, and PRINT_BRIDGE_TOKEN.
)

echo.
echo Installing Scheduled Task "%TASK_NAME%"...
schtasks /Delete /TN "%TASK_NAME%" /F >nul 2>&1
schtasks /Create /TN "%TASK_NAME%" /TR "\"%RUNNER%\"" /SC ONLOGON /RL LIMITED /F
if %ERRORLEVEL% NEQ 0 (
    echo ERROR: Could not install the Scheduled Task.
    pause
    exit /b 1
)

echo Starting the print spooler now...
schtasks /Run /TN "%TASK_NAME%" >nul 2>&1

echo.
echo DONE. Zorro's print spooler is installed for Windows.
echo It will start when this Windows user logs in and the runner restarts it if it exits.
echo.
echo Useful commands:
echo   schtasks /Query /TN "%TASK_NAME%"
echo   schtasks /Run /TN "%TASK_NAME%"
echo   schtasks /Delete /TN "%TASK_NAME%" /F
echo.
pause
