@echo off
cd /d "%~dp0"

echo ========================================
echo Shopping App - First Time Setup
echo ========================================
echo.

set "PY_CMD=python"

python --version >nul 2>nul
if errorlevel 1 (
    py -3 --version >nul 2>nul
    if errorlevel 1 (
        echo Python was not found.
        echo Please install Python 3.10 or newer first.
        pause
        exit /b 1
    )
    set "PY_CMD=py -3"
)

echo [1/5] Creating virtual environment...
if not exist "venv\Scripts\python.exe" (
    %PY_CMD% -m venv venv
)

if not exist "venv\Scripts\python.exe" (
    echo Failed to create venv.
    pause
    exit /b 1
)

echo.
echo [2/5] Upgrading pip...
"venv\Scripts\python.exe" -m pip install --upgrade pip

echo.
echo [3/5] Installing Python packages...
"venv\Scripts\python.exe" -m pip install -r requirements-windows.txt

if errorlevel 1 (
    echo.
    echo Package installation failed.
    pause
    exit /b 1
)

echo.
echo [4/5] Configuring local environment...
"venv\Scripts\python.exe" configure_local.py

if errorlevel 1 (
    echo.
    echo Local configuration failed.
    pause
    exit /b 1
)

echo.
echo [5/5] Checking ODBC driver...
"venv\Scripts\python.exe" -c "import pyodbc; print('Installed ODBC drivers:'); print('\n'.join(pyodbc.drivers()))"

echo.
echo ========================================
echo Setup finished.
echo Next time, double click start_site.bat
echo ========================================
echo.

pause