#!/bin/bash

echo "๐Ÿš€ Activating Quantum-API Startup"

# Start FastAPI in background on port 7860
echo "๐ŸŒ€ Starting FastAPI (API backend) on port 7860..."
uvicorn api.main:app --host 0.0.0.0 --port 7860 &> fastapi.log &

# Capture PID for later kill
FASTAPI_PID=$!

# Wait a bit to ensure it starts
sleep 3

# Check if FastAPI is running
if ! ps -p $FASTAPI_PID > /dev/null; then
    echo "โŒ FastAPI failed to start. Logs:"
    cat fastapi.log
    exit 1
else
    echo "โœ… FastAPI is running as PID $FASTAPI_PID"
fi

# Start Streamlit in foreground (required by Hugging Face)
echo "โœจ Launching Streamlit frontend on port 7860 (Hugging Face expects this)..."
streamlit run app/app.py --server.port 8000

# Optional cleanup
echo "๐Ÿงน Shutting down FastAPI PID $FASTAPI_PID..."
kill $FASTAPI_PID