#!/bin/bash

# LangFuse Setup Script for darkstories
# This script helps you set up and configure LangFuse locally

set -e

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

echo "🚀 darkstories LangFuse Setup"
echo "=============================="
echo

# Check if Docker is running
if ! docker info > /dev/null 2>&1; then
    echo "❌ Docker is not running. Please start Docker first."
    exit 1
fi

# Check if docker-compose is available
if ! command -v docker-compose > /dev/null 2>&1; then
    echo "❌ docker-compose is not installed. Please install it first."
    exit 1
fi

# Function to start LangFuse
start_langfuse() {
    echo "📦 Starting LangFuse services..."
    docker-compose up -d
    
    echo "⏳ Waiting for services to be ready..."
    sleep 10
    
    # Check if services are healthy
    if docker-compose ps | grep -q "Up (healthy)"; then
        echo "✅ LangFuse is running!"
        echo
        echo "🌐 Access LangFuse at: http://localhost:3000"
        echo "📧 Default login: admin@darkstories.local"
        echo "🔑 Default password: admin123"
        echo
        echo "Next steps:"
        echo "1. Login to LangFuse web interface"
        echo "2. Go to Settings → Projects to get your API keys"
        echo "3. Add the keys to your main darkstories .env file:"
        echo "   ENABLE_LANGFUSE=true"
        echo "   LANGFUSE_PUBLIC_KEY=pk-lf-your-key-here"
        echo "   LANGFUSE_SECRET_KEY=sk-lf-your-secret-here"
        echo "   LANGFUSE_HOST=http://localhost:3000"
        echo "4. Restart your darkstories application"
    else
        echo "⚠️  Some services may still be starting. Check with: docker-compose ps"
    fi
}

# Function to stop LangFuse
stop_langfuse() {
    echo "🛑 Stopping LangFuse services..."
    docker-compose down
    echo "✅ LangFuse stopped."
}

# Function to show status
show_status() {
    echo "📊 LangFuse Service Status:"
    echo "=========================="
    docker-compose ps
}

# Function to show logs
show_logs() {
    echo "📝 LangFuse Logs (press Ctrl+C to exit):"
    echo "========================================"
    docker-compose logs -f
}

# Function to reset LangFuse (removes all data)
reset_langfuse() {
    echo "⚠️  This will remove ALL LangFuse data!"
    read -p "Are you sure? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo "🗑️  Removing LangFuse and all data..."
        docker-compose down -v
        echo "✅ Reset complete. Run 'start' to begin fresh."
    else
        echo "❌ Reset cancelled."
    fi
}

# Main script logic
case "${1:-}" in
    "start")
        start_langfuse
        ;;
    "stop")
        stop_langfuse
        ;;
    "status")
        show_status
        ;;
    "logs")
        show_logs
        ;;
    "reset")
        reset_langfuse
        ;;
    "restart")
        stop_langfuse
        sleep 2
        start_langfuse
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status|logs|reset}"
        echo
        echo "Commands:"
        echo "  start   - Start LangFuse services"
        echo "  stop    - Stop LangFuse services"
        echo "  restart - Restart LangFuse services"
        echo "  status  - Show service status"
        echo "  logs    - Show service logs"
        echo "  reset   - Remove all data and reset"
        echo
        exit 1
        ;;
esac
