#!/bin/bash

echo "🔍 Démarrage du Microservice de Recherche Intelligente (Symfony)"
echo ""

# Vérifications pré-démarrage
if [ ! -f "composer.json" ]; then
    echo "❌ Pas dans le bon dossier"
    exit 1
fi

if [ ! -d "vendor" ]; then
    echo "❌ Dépendances non installées. Exécutez: ./install.sh"
    exit 1
fi

# Configuration
PORT=$(grep -E '^SERVER_PORT=' .env | cut -d '=' -f2 || echo "8003")
OPENAI_KEY=$(grep -E '^OPENAI_API_KEY=' .env | cut -d '=' -f2)

# Nettoyer le cache
echo "🧹 Nettoyage du cache..."
php bin/console cache:clear --env=prod --no-debug > /dev/null 2>&1

# Informations
echo "📊 Configuration:"
echo "   Port: $PORT"
echo "   IA: $([ "$OPENAI_KEY" != "your_openai_api_key_here" ] && [ -n "$OPENAI_KEY" ] && echo "✅ Activée" || echo "⚠️  Fallback")"
echo "   API: $(grep -E '^MAIN_API_BASE_URL=' .env | cut -d '=' -f2)"
echo ""
echo "🚀 Service démarré sur: http://localhost:$PORT"
echo ""
echo "📡 Endpoints disponibles:"
echo "   POST /api/search/intelligent  - Recherche IA"
echo "   GET  /api/search?q=query      - Recherche simple"
echo "   GET  /api/search/health       - Status service"
echo ""
echo "💡 Test rapide:"
echo "   curl http://localhost:$PORT/api/search/health"
echo ""
echo "Appuyez sur Ctrl+C pour arrêter"
echo ""

# Démarrage du serveur PHP
php -S localhost:$PORT -t public/