#!/bin/bash

# Start the backend server
echo "🚀 Starting the backend server..."
cd api
npm install
node server.js &
SERVER_PID=$!
cd ..

# Wait for the server to start
echo "⏳ Waiting for the server to start..."
sleep 2

# Start a simple HTTP server for the demo
echo "🚀 Starting HTTP server for the demo..."
python3 -m http.server 8080 &
HTTP_PID=$!

# Function to handle script termination
cleanup() {
  echo "🛑 Stopping all processes..."
  kill $SERVER_PID $HTTP_PID
  exit 0
}

# Register the cleanup function for script termination
trap cleanup SIGINT SIGTERM

echo "✅ All services started!"
echo "📝 Standalone Demo: http://localhost:8080/standalone-demo.html"
echo "📡 Backend server: http://localhost:3001"
echo "Press Ctrl+C to stop all services"

# Keep the script running
wait