#!/usr/bin/env bash
# Polleo Demand — Demo launcher (macOS / Linux)
set -e

cd "$(dirname "$0")"

# First-run setup: install deps if streamlit is missing
if ! python3 -c "import streamlit" 2>/dev/null; then
    echo "[setup] First run — installing requirements..."
    python3 -m pip install --upgrade pip
    python3 -m pip install -r requirements.txt
fi

# First-run setup: generate dummy data if missing
if [ ! -f "data/sales_clean.csv" ]; then
    echo "[setup] Generating demo data..."
    python3 generate_demo_data.py
fi

# First-run setup: build initial forecast (gives DP / Revenue pages data on first launch)
if [ ! -f "data/Polleo_Demand_Plan.xlsx" ]; then
    echo "[setup] Building initial forecast (~60s)..."
    python3 build_initial_forecast.py || echo "[WARN] forecast build failed; click Run forecast inside the app later."
fi

echo
echo "[run] Launching Streamlit on http://localhost:8501 ..."
echo "      Press Ctrl+C to stop."
echo
exec python3 -m streamlit run app.py
