#!/bin/bash
# Start the complete Paradisomatch system
# - Web server for map viewer and criteria manager
# - API server for automated weight updates

cd "$(dirname "$0")"

echo "🚀 Starting Paradisomatch System..."
echo ""

# Kill existing servers
echo "📌 Cleaning up existing servers..."
lsof -ti:8000 | xargs kill -9 2>/dev/null
lsof -ti:5001 | xargs kill -9 2>/dev/null
lsof -ti:5002 | xargs kill -9 2>/dev/null
sleep 1

# Prefer venv Python if available
PY_BIN="../venv/bin/python3.14"
if [ ! -x "$PY_BIN" ]; then
  PY_BIN="../venv/bin/python3"
fi
if [ ! -x "$PY_BIN" ]; then
  PY_BIN="python3"
fi

# Start web server for HTML files
echo "🌐 Starting web server on http://localhost:8000"
"$PY_BIN" -m http.server 8000 > /dev/null 2>&1 &
WEB_PID=$!

# Start API server for criteria updates
echo "🔧 Starting API server on http://localhost:5002"
"$PY_BIN" criteria_api.py > /tmp/criteria_api.log 2>&1 &
API_PID=$!

sleep 2

echo ""
echo "✅ System Started!"
echo ""
echo "📊 Map Viewer:        http://localhost:8000/map_viewer_advanced.html"
echo "🎯 Criteria Manager:  http://localhost:8000/criteria_manager.html"
echo "🔧 API Server:        http://localhost:5002"
echo ""
echo "💡 Tip: Use the Criteria Manager to adjust weights and they'll be"
echo "   automatically applied to the Python files!"
echo ""
echo "📝 API logs: tail -f /tmp/criteria_api.log"
echo ""

# Open browser
open "http://localhost:8000/criteria_manager.html"
