#!/bin/bash

# Build the enhanced SDK
echo "📦 Building the enhanced SDK..."
cd enhanced-sdk
npm install
bash build.js
cd ..

# 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..."
cd enhanced-demo
python3 -m http.server 8080 &
HTTP_PID=$!
cd ..

# 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 "📝 Enhanced Demo: http://localhost:8080"
echo "📡 Backend server: http://localhost:3001"
echo "Press Ctrl+C to stop all services"

# Keep the script running
wait