@echo off
setlocal ENABLEDELAYEDEXPANSION

:: Set project directory to script's location
set "PROJECT_DIR=%~dp0"
cd /d "%PROJECT_DIR%"

echo -----------------------------------------
echo Checking virtual environment...
echo -----------------------------------------

:: Create venv if missing
if not exist "venv\Scripts\python.exe" (
    echo Creating virtual environment...
    python -m venv venv
)

:: Activate venv
call "venv\Scripts\activate.bat"

echo -----------------------------------------
echo Upgrading pip...
echo -----------------------------------------
python -m pip install --upgrade pip

echo -----------------------------------------
echo Installing Python prerequisites...
echo -----------------------------------------

pip install flask selenium webdriver-manager beautifulsoup4 playwright

echo -----------------------------------------
echo Installing Playwright browser binaries...
echo -----------------------------------------
playwright install || (
    echo ERROR: Playwright install failed! Trying chromium only...
    playwright install chromium
)

echo -----------------------------------------
echo All dependencies installed successfully!
echo Launching server...
echo -----------------------------------------

:: Start Flask server with persistent window
start "" cmd /k "venv\Scripts\python.exe scraper_service.py"

endlocal
exit /b 0
