@echo off
setlocal enableextensions
cd /d "%~dp0"
echo ============================================================
echo    Flyff Bot - one-time setup
echo    This installs everything needed. Takes a few minutes.
echo ============================================================
echo.

REM ---------- 0) Sanity: are the bot files actually here? ----------
REM If a core file is missing you're almost certainly running install.bat from INSIDE the .zip
REM (Windows extracts only the one file to a temp folder). EXTRACT the whole folder first.
if not exist "flyffbot.py" (
  echo    ERROR: the bot files are not in this folder ^(flyffbot.py is missing^).
  echo.
  echo    You are probably running install.bat from INSIDE the .zip preview.
  echo    Please UNZIP / EXTRACT the whole folder first, then run install.bat
  echo    from the EXTRACTED folder ^(not from inside the zip^).
  echo.
  pause & exit /b 1
)

REM ---------- 1) Python (3.9+) ----------
echo [1/5] Checking for Python 3.9+ ...
set "PY="
python -c "import sys; sys.exit(0 if sys.version_info>=(3,9) else 1)" 1>nul 2>nul && set "PY=python"
if not defined PY ( py -3 -c "import sys; sys.exit(0 if sys.version_info>=(3,9) else 1)" 1>nul 2>nul && set "PY=py -3" )
if not defined PY (
  echo        No suitable Python found. Trying to install it with winget...
  where winget 1>nul 2>nul
  if errorlevel 1 (
    echo.
    echo        winget is not available on this PC. Please install Python 3.12 manually:
    echo            https://www.python.org/downloads/
    echo        IMPORTANT: tick "Add Python to PATH" during setup, then run install.bat again.
    echo.
    pause & exit /b 1
  )
  winget install -e --id Python.Python.3.12 --accept-source-agreements --accept-package-agreements
  echo.
  echo        Python was installed. Please CLOSE this window and run install.bat
  echo        ONE more time so the new PATH takes effect.
  echo.
  pause & exit /b 0
)
echo        OK - using: %PY%

REM ---------- 2) Isolated environment ----------
echo [2/5] Creating an isolated environment (.venv)...
if not exist ".venv\Scripts\python.exe" (
  %PY% -m venv .venv
  if errorlevel 1 ( echo        ERROR: could not create the environment. & pause & exit /b 1 )
)
set "VPY=.venv\Scripts\python.exe"
echo        OK

REM ---------- 3) Python packages ----------
echo [3/5] Installing Python packages (numpy, opencv, playwright, pytesseract)...
echo        (this can take a few minutes - please wait)
"%VPY%" -m pip install --upgrade pip 1>nul 2>nul
if exist "requirements.txt" (
  "%VPY%" -m pip install -r requirements.txt
) else (
  echo        requirements.txt not found - installing the package list directly...
  "%VPY%" -m pip install playwright opencv-python numpy pytesseract
)
if errorlevel 1 ( echo        ERROR: package install failed - check your internet connection. & pause & exit /b 1 )
echo        OK

REM ---------- 4) Chromium browser + Tesseract OCR ----------
echo [4/5] Downloading the Chromium browser...
"%VPY%" -m playwright install chromium
if errorlevel 1 ( echo        ERROR: Chromium download failed - check your internet connection. & pause & exit /b 1 )
echo        Installing Tesseract OCR (optional - exact HP/MP/Exp text readout)...
where winget 1>nul 2>nul
if errorlevel 1 (
  echo        winget not available - skipping Tesseract. The bot still works fully;
  echo        the header's OCR row will just show '-'. You can install it later.
) else (
  winget install -e --id UB-Mannheim.tesseract-ocr --accept-source-agreements --accept-package-agreements
)
echo        OK

REM ---------- 5) Verify everything really works ----------
echo [5/5] Verifying the installation...
"%VPY%" -c "import numpy, cv2; from playwright.sync_api import sync_playwright" 1>nul 2>nul
if errorlevel 1 ( echo        ERROR: Python packages did not import. Try running install.bat again. & pause & exit /b 1 )
"%VPY%" -c "from playwright.sync_api import sync_playwright; import os,sys; p=sync_playwright().start(); ok=os.path.exists(p.chromium.executable_path); p.stop(); sys.exit(0 if ok else 1)" 1>nul 2>nul
if errorlevel 1 ( echo        ERROR: the Chromium browser is missing. Try running install.bat again. & pause & exit /b 1 )
echo        OK - everything checks out.

echo.
echo ============================================================
echo    DONE!  Now double-click  flyffbot.bat  to start.
echo    A browser window opens - log into YOUR OWN Flyff account,
echo    get in-game, then use the control panel (top-right).
echo    (First, bind your hotbar keys - see START_HERE.txt)
echo ============================================================
echo.
pause
