#!/bin/bash

echo "🚀 Starting Orchestry (All-in-One)..."
echo ""
echo "📦 Installing dependencies..."
npm install

echo ""
echo "🔧 Building TypeScript..."
cd server && npm run build && cd ..

echo ""
echo "🎯 Starting services..."
echo "  - Web API Server on http://localhost:3001"
echo "  - Web UI on http://localhost:3001"
echo ""

# Start API server in background
cd server && npm run dev:web &
SERVER_PID=$!

# Wait for server to start
sleep 3

# Start web UI
cd ../web && npm run dev &
WEB_PID=$!

# Open browser automatically (like Serena)
sleep 2
echo "🌐 Opening browser..."
open http://localhost:3001

echo ""
echo "✅ Orchestry is running!"
echo "   Server PID: $SERVER_PID"
echo "   Web PID: $WEB_PID"
echo ""
echo "Press Ctrl+C to stop all services"

# Wait for interrupt
trap "echo ''; echo '🛑 Stopping services...'; kill $SERVER_PID $WEB_PID; exit" INT
wait