@echo off
REM ============================================================
REM  Zorro's Print Agent - Windows Auto-Installer
REM  Double-click this file. It installs everything automatically.
REM ============================================================

echo.
echo  ==========================================
echo    ZORRO'S PRINT AGENT - AUTO INSTALLER
echo  ==========================================
echo.

REM --- Check if Python is already installed ---
python --version >nul 2>&1
IF %ERRORLEVEL% EQU 0 (
    echo [OK] Python already installed.
    goto :install_deps
)

echo [!] Python not found. Installing automatically...
echo.

REM --- Try winget (Windows 10/11 built-in) ---
winget --version >nul 2>&1
IF %ERRORLEVEL% EQU 0 (
    echo Installing Python via winget...
    winget install -e --id Python.Python.3.12 --silent --accept-package-agreements --accept-source-agreements
    goto :refresh_path
)

REM --- Fallback: download Python installer ---
echo winget not available. Downloading Python installer...
curl -L -o "%TEMP%\python_installer.exe" "https://www.python.org/ftp/python/3.12.4/python-3.12.4-amd64.exe"
echo Running Python installer (follow the prompts)...
echo IMPORTANT: Check "Add Python to PATH" on the first screen!
"%TEMP%\python_installer.exe" /quiet PrependPath=1
echo.

:refresh_path
REM Refresh PATH so python is found
call refreshenv >nul 2>&1
python --version >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
    echo.
    echo [!] Python install may need a restart.
    echo     Close this window, restart the computer, then double-click this file again.
    pause
    exit /b 1
)
echo [OK] Python installed successfully.

:install_deps
echo.
echo [..] Installing required packages...
python -m pip install requests --quiet --disable-pip-version-check
echo [OK] Packages ready.

echo.
echo [..] Checking phone-order readiness before setup...
python "%~dp0zorros_print_agent.py" --status
echo.

IF "%PRINT_BRIDGE_TOKEN%"=="" (
    set /p PRINT_BRIDGE_TOKEN=Enter print bridge token from Veli:
)
IF "%PRINT_BRIDGE_TOKEN%"=="" (
    echo [!] 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"

echo.
echo [..] Testing printer at %PRINTER_IP%:%PRINTER_PORT%...
python -c "import os,socket; ip=os.environ.get('PRINTER_IP','192.168.213.109'); port=int(os.environ.get('PRINTER_PORT','9100')); s=socket.create_connection((ip,port),timeout=3); s.close(); print('[OK] Printer REACHABLE')" 2>nul
IF %ERRORLEVEL% NEQ 0 echo [!] Printer not reachable - make sure this computer is on the store WiFi.

echo [..] Testing internet connection...
python -c "import urllib.request; urllib.request.urlopen('https://zorrosorder.com/api/print/health',timeout=5); print('[OK] Connected to Zorros server')" 2>nul
IF %ERRORLEVEL% NEQ 0 echo [!] Cannot reach server - check internet connection.

echo.
set /p RUN_TEST_PRINT=Print one local-only setup test ticket now? [Y/N]:
IF /I "%RUN_TEST_PRINT%"=="Y" (
    python "%~dp0zorros_print_agent.py" --test-print
)

echo.
set /p RUN_BRIDGE_TEST=Print one Railway bridge setup ticket now? [Y/N]:
IF /I "%RUN_BRIDGE_TEST%"=="Y" (
    python "%~dp0zorros_print_agent.py" --test-bridge-print
)

echo.
echo ==========================================
echo   STARTING PRINT AGENT - DO NOT CLOSE
echo   Every new order will now print here.
echo   Press Ctrl+C to stop.
echo ==========================================
echo.

REM Run the agent from same folder as this .bat
python "%~dp0zorros_print_agent.py"
pause
