#!/bin/bash

# Build the fixed SDK
echo "📦 Building the fixed SDK..."
cd fixed-sdk
npm install
bash manual-build.sh
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 HTML test
echo "🚀 Starting HTTP server for HTML test..."
cd html-test
python3 -m http.server 8000 &
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 "📝 HTML test: http://localhost:8000"
echo "📡 Backend server: http://localhost:3001"
echo "Press Ctrl+C to stop all services"

# Keep the script running
wait