#!/bin/bash
#
# Crumbforest Start Script
# Startet alle Docker Container
#

set -e

echo "🦉 Starte Crumbforest..."
echo ""

cd compose

# Check if containers exist
if docker compose ps | grep -q "crumb"; then
    echo "Container existieren bereits, starte sie..."
    docker compose start
else
    echo "Erstelle und starte Container..."
    docker compose up -d
fi

echo ""
echo "Warte auf Services..."
sleep 3

# Wait for FastAPI
max_attempts=30
attempt=0
while [ $attempt -lt $max_attempts ]; do
    if curl -s http://localhost:8000/health > /dev/null 2>&1; then
        echo "✓ FastAPI ist bereit"
        break
    fi
    attempt=$((attempt + 1))
    echo -n "."
    sleep 1
done

if [ $attempt -eq $max_attempts ]; then
    echo ""
    echo "✗ FastAPI Timeout - prüfe Logs mit: ./logs.sh app"
    exit 1
fi

cd ..

echo ""
echo "✓ System läuft!"
echo ""
echo "URLs:"
echo "  - FastAPI:     http://localhost:8000"
echo "  - Admin:       http://localhost:8000/de/login"
echo "  - Qdrant:      http://localhost:6333/dashboard"
echo "  - API Docs:    http://localhost:8000/docs"
echo ""
echo "Befehle:"
echo "  ./logs.sh     - Logs ansehen"
echo "  ./stop.sh     - System stoppen"
echo "  ./test.sh     - Tests ausführen"
echo ""
