#!/bin/bash

# Gemini FAQ App Startup Script
# This script starts all the required services for the application

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

print_status() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Function to check if command exists
command_exists() {
    command -v "$1" >/dev/null 2>&1
}

# Function to check if DDEV is running
check_ddev_status() {
    if ddev status >/dev/null 2>&1; then
        return 0
    else
        return 1
    fi
}

# Function to cleanup background processes on exit
cleanup() {
    print_status "Shutting down services..."
    if [ ! -z "$VITE_PID" ]; then
        kill $VITE_PID 2>/dev/null || true
        print_status "Stopped Vite development server"
    fi
    if [ ! -z "$PYTHON_PID" ]; then
        kill $PYTHON_PID 2>/dev/null || true
        print_status "Stopped Python RAG service"
    fi
    print_success "All services stopped. Goodbye!"
}

# Set up cleanup trap
trap cleanup EXIT INT TERM

print_status "Starting Gemini FAQ App services..."
echo "===================================="

# Check if setup has been run
if [ ! -f .env ]; then
    print_error "Application not set up yet. Please run ./setup.sh first."
    exit 1
fi

# Check if DDEV is running
if ! check_ddev_status; then
    print_status "Starting DDEV..."
    ddev start
fi

# Start Vite development server
print_status "Starting Vite development server..."
ddev exec "npm run dev" &
VITE_PID=$!
print_success "Vite development server started (PID: $VITE_PID)"

# Wait a moment for Vite to start
sleep 3

# Start Python RAG service
print_status "Starting Python RAG service..."
cd services/rag-service
./start.sh background
cd ../..
print_success "Python RAG service started"

# Wait a moment for services to fully start
sleep 5

# Display service status
echo ""
echo "===================================="
print_success "🚀 All services are now running!"
echo "===================================="

# Get DDEV URL
DDEV_URL=$(ddev describe | grep "https://" | head -1 | awk '{print $2}')

echo ""
print_status "Access your application at:"
echo "  • Chatbot Interface: ${DDEV_URL}"
echo "  • Content Management: ${DDEV_URL}/content-management"
echo "  • Laravel API: ${DDEV_URL}/api/content/"
echo "  • Python RAG Service: http://localhost:5002"
echo ""

# Test services
print_status "Testing service health..."

# Test Python service
if curl -s http://127.0.0.1:5002/health >/dev/null 2>&1; then
    print_success "✓ Python RAG service is healthy"
else
    print_warning "⚠ Python RAG service may still be starting up"
fi

# Test Laravel API
if curl -s "${DDEV_URL}/api/content/status" >/dev/null 2>&1; then
    print_success "✓ Laravel API is responding"
else
    print_warning "⚠ Laravel API may still be starting up"
fi

echo ""
print_status "Press Ctrl+C to stop all services"
print_warning "Keep this terminal open to maintain the running services"
echo ""

# Keep the script running and wait for user interrupt
while true; do
    sleep 1
done
