#!/bin/bash
# Start both backend and frontend services for SBA gameplay testing

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOG_DIR="$SCRIPT_DIR/logs"

# Create logs directory if it doesn't exist
mkdir -p "$LOG_DIR"

echo "🚀 Starting SBA Gameplay Services..."
echo ""

# Start Backend
echo "📡 Starting Backend (FastAPI + Socket.io)..."
cd "$SCRIPT_DIR/backend"
uv run python -m app.main > "$LOG_DIR/backend.log" 2>&1 &
BACKEND_PID=$!
echo "   Backend started with PID: $BACKEND_PID"
echo "   Logs: $LOG_DIR/backend.log"
echo "   API: http://localhost:8000"
echo ""

# Wait for backend to initialize
sleep 3

# Start Frontend
echo "🎨 Starting Frontend (Nuxt 3)..."
cd "$SCRIPT_DIR/frontend-sba"
npm run dev > "$LOG_DIR/frontend.log" 2>&1 &
FRONTEND_PID=$!
echo "   Frontend started with PID: $FRONTEND_PID"
echo "   Logs: $LOG_DIR/frontend.log"
echo "   URL: http://localhost:3000"
echo ""

# Save PIDs for stop script
echo "$BACKEND_PID" > "$LOG_DIR/backend.pid"
echo "$FRONTEND_PID" > "$LOG_DIR/frontend.pid"

echo "✅ Services started successfully!"
echo ""
echo "📊 To view logs:"
echo "   Backend:  tail -f $LOG_DIR/backend.log"
echo "   Frontend: tail -f $LOG_DIR/frontend.log"
echo ""
echo "🛑 To stop services: ./stop-services.sh"
