@echo off
title Polleo Demand Planning
color 1F

echo.
echo   ========================================
echo     POLLEO DEMAND PLANNING
echo   ========================================
echo.

:: Load secrets from .env (POLLEO_SLACK_TOKEN=xoxb-...).
:: `eol=#` skips comment lines; empty lines are ignored by /f.
if exist .env (
    for /f "usebackq eol=# tokens=1,* delims==" %%a in (".env") do (
        if not "%%a"=="" set "%%a=%%b"
    )
)

:: Check Python
py --version >nul 2>&1
if %errorlevel% neq 0 (
    echo   [!] Python not found.
    echo.
    echo   Installing Python is a one-time step:
    echo   1. Go to python.org
    echo   2. Download Python 3.12
    echo   3. IMPORTANT: Check "Add to PATH" during install
    echo   4. Restart your computer
    echo   5. Double-click this file again
    echo.
    pause
    exit /b
)

echo   [OK] Python found
py --version

:: Check if dependencies are installed (check for streamlit)
py -c "import streamlit" >nul 2>&1
if %errorlevel% neq 0 (
    echo.
    echo   [..] First run - installing dependencies...
    echo       This takes 1-2 minutes, only happens once.
    echo.
    py -m pip install -r requirements.txt --break-system-packages
    if %errorlevel% neq 0 (
        echo.
        echo   [!] Install failed. Try running as administrator.
        pause
        exit /b
    )
    echo.
    echo   [OK] Dependencies installed!
)

:: Check if slack_sdk is installed
py -c "import slack_sdk" >nul 2>&1
if %errorlevel% neq 0 (
    echo.
    echo   [..] Installing Slack integration...
    py -m pip install slack_sdk --break-system-packages
    echo   [OK] Slack SDK installed!
)

echo   [OK] All dependencies ready
echo.
echo   Starting Polleo Demand Planning...
echo   Your browser will open automatically.
echo.
echo   To stop: close this window or press Ctrl+C
echo   ========================================
echo.

py -m streamlit run app.py --server.port 8501
pause
