#!/bin/bash

# Excel Filter Tool - Streamlit App Runner
# This script starts the Streamlit web application

echo "========================================"
echo "  Excel Filter Tool - Web Application"
echo "========================================"
echo ""

# Check if Python is installed
if ! command -v python &> /dev/null; then
    echo "Error: Python is not installed or not in PATH"
    exit 1
fi

# Check if we're in the correct directory
if [ ! -f "streamlit_app.py" ]; then
    echo "Error: streamlit_app.py not found"
    echo "Please run this script from the project root directory"
    exit 1
fi

# Create virtual environment if it doesn't exist
if [ ! -d "venv" ]; then
    echo "Creating virtual environment..."
    python -m venv venv
fi

# Activate virtual environment
echo "Activating virtual environment..."
source venv/bin/activate 2>/dev/null || source venv/Scripts/activate 2>/dev/null

# Install/upgrade pip
echo "Upgrading pip..."
pip install --upgrade pip -q

# Install requirements
echo "Installing dependencies..."
pip install -r excel_filter/requirements.txt -q
pip install streamlit -q

echo ""
echo "Starting Streamlit server..."
echo "The app will open in your browser automatically."
echo "Press Ctrl+C to stop the server."
echo ""

# Run Streamlit app
streamlit run streamlit_app.py --server.headless=true

# Deactivate virtual environment on exit
deactivate