#!/bin/bash

# BuzzTrail Video Demo - Local Development Quick Start
echo "🚀 Starting BuzzTrail Video Demo Local Development Environment"
echo ""

# Check if we're in the right directory
if [ ! -f "local-dev.html" ]; then
    echo "❌ Error: local-dev.html not found. Please run this script from the buzztrail-video-demo directory."
    exit 1
fi

# Start the fake Tavus server in the background
echo "📡 Starting fake Tavus server on port 3001..."
cd ../../..
if command -v pnpm &> /dev/null; then
    echo "   Using pnpm nx serve..."
    pnpm nx serve fake-tavus-server &
    SERVER_PID=$!
    echo "   Server PID: $SERVER_PID"
else
    echo "   Using node directly..."
    node apps/fake-tavus-server/simple-server.js &
    SERVER_PID=$!
    echo "   Server PID: $SERVER_PID"
fi

# Wait a moment for server to start
sleep 2

# Check if server is running
if curl -s http://localhost:3001/health > /dev/null; then
    echo "✅ Fake Tavus server is running!"
else
    echo "❌ Failed to start fake Tavus server"
    exit 1
fi

# Go back to demo directory
cd apps/demos/buzztrail-video-demo

# Start a simple HTTP server for the HTML page
echo ""
echo "🌐 Starting HTTP server for local development page..."
if command -v python3 &> /dev/null; then
    echo "   Using Python HTTP server on port 8080..."
    python3 -m http.server 8080 &
    HTTP_PID=$!
elif command -v python &> /dev/null; then
    echo "   Using Python 2 HTTP server on port 8080..."
    python -m SimpleHTTPServer 8080 &
    HTTP_PID=$!
elif command -v npx &> /dev/null; then
    echo "   Using npx serve on port 8080..."
    npx serve -p 8080 . &
    HTTP_PID=$!
else
    echo "⚠️  No HTTP server available. Please open local-dev.html directly in your browser."
    HTTP_PID=""
fi

# Wait a moment for HTTP server to start
sleep 2

echo ""
echo "🎯 Development Environment Ready!"
echo ""
echo "📋 Quick Access:"
echo "   • Local dev page: http://localhost:8080/local-dev.html"
echo "   • Fake server health: http://localhost:3001/health"
echo "   • Server logs: Check terminal output"
echo ""
echo "🛠️  Development Workflow:"
echo "   1. Open http://localhost:8080/local-dev.html in your browser"
echo "   2. Use development controls to adjust settings"
echo "   3. Click 'Talk to Tom' to test conversation flow"
echo "   4. Edit src/wix-demo.js for changes"
echo "   5. Run 'pnpm nx build:wix demos' to rebuild"
echo ""
echo "🎨 BuzzTrail Brand Colors:"
echo "   • Primary: #a855f7 (purple)"
echo "   • Secondary: #ec4899 (pink)"
echo "   • Gradient: linear-gradient(135deg, #a855f7 0%, #ec4899 100%)"
echo ""

# Open the browser automatically (macOS)
if [[ "$OSTYPE" == "darwin"* ]]; then
    echo "🌏 Opening browser..."
    sleep 1
    open http://localhost:8080/local-dev.html
fi

echo "📝 View LOCAL_DEVELOPMENT.md for detailed instructions"
echo ""
echo "⏹️  To stop all servers, press Ctrl+C"

# Create cleanup function
cleanup() {
    echo ""
    echo "🛑 Shutting down development environment..."
    if [ ! -z "$SERVER_PID" ]; then
        echo "   Stopping fake Tavus server (PID: $SERVER_PID)..."
        kill $SERVER_PID 2>/dev/null
    fi
    if [ ! -z "$HTTP_PID" ]; then
        echo "   Stopping HTTP server (PID: $HTTP_PID)..."
        kill $HTTP_PID 2>/dev/null
    fi
    echo "✅ Cleanup complete"
    exit 0
}

# Set up signal handlers for cleanup
trap cleanup SIGINT SIGTERM

# Keep script running
echo "💡 Tip: Edit src/wix-demo.js and rebuild with 'pnpm nx build:wix demos'"
echo "🔄 Press Ctrl+C to stop all servers"
echo ""

# Wait for user to stop
while true; do
    sleep 1
done