#!/bin/bash

# Consolidated Services Management Script for Staging/Production
# Manages all application services in one place

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}[SERVICES]${NC} $1"
}

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

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

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

# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SERVICES_DIR="$SCRIPT_DIR"

show_usage() {
    echo "Usage: $0 {start|stop|restart|status|logs}"
    echo ""
    echo "Commands:"
    echo "  start    - Start all services"
    echo "  stop     - Stop all services"
    echo "  restart  - Restart all services"
    echo "  status   - Show service status"
    echo "  logs     - Show service logs"
    echo ""
}

start_services() {
    print_status "Starting application services..."
    
    # Start RAG Service
    print_status "Starting RAG Service..."
    cd "$SERVICES_DIR/rag-service"
    if [ -f "start.sh" ]; then
        ./start.sh background
        print_success "RAG Service started"
    else
        print_error "RAG Service start script not found"
        return 1
    fi
    
    cd "$SERVICES_DIR"
    
    # Add other services here as needed
    # Example:
    # print_status "Starting other services..."
    
    print_success "All services started successfully"
}

stop_services() {
    print_status "Stopping application services..."
    
    # Stop RAG Service
    print_status "Stopping RAG Service..."
    cd "$SERVICES_DIR/rag-service"
    if [ -f "stop.sh" ]; then
        ./stop.sh
        print_success "RAG Service stopped"
    else
        print_warning "RAG Service stop script not found"
    fi
    
    cd "$SERVICES_DIR"
    
    # Add other services here as needed
    
    print_success "All services stopped"
}

restart_services() {
    print_status "Restarting application services..."
    stop_services
    sleep 2
    start_services
    print_success "All services restarted"
}

show_status() {
    print_status "Service Status:"
    echo ""
    
    # Check RAG Service
    echo "RAG Service:"
    cd "$SERVICES_DIR/rag-service"
    if [ -f "rag_service.pid" ]; then
        PID=$(cat rag_service.pid)
        if kill -0 "$PID" 2>/dev/null; then
            echo -e "  Status: ${GREEN}Running${NC} (PID: $PID)"
            
            # Test health endpoint
            if curl -s http://localhost:5002/rag-service/health >/dev/null 2>&1; then
                echo -e "  Health: ${GREEN}Healthy${NC}"
            else
                echo -e "  Health: ${YELLOW}Unreachable${NC}"
            fi
        else
            echo -e "  Status: ${RED}Stopped${NC} (PID file exists but process not running)"
        fi
    else
        echo -e "  Status: ${RED}Stopped${NC}"
    fi
    echo ""
    
    cd "$SERVICES_DIR"
}

show_logs() {
    print_status "Service Logs:"
    echo ""
    
    # RAG Service logs
    echo "RAG Service logs (last 20 lines):"
    cd "$SERVICES_DIR/rag-service"
    if [ -f "rag_service.log" ]; then
        tail -20 rag_service.log
    else
        echo "No log file found"
    fi
    echo ""
    
    cd "$SERVICES_DIR"
}

# Main script logic
case "${1:-}" in
    start)
        start_services
        ;;
    stop)
        stop_services
        ;;
    restart)
        restart_services
        ;;
    status)
        show_status
        ;;
    logs)
        show_logs
        ;;
    *)
        show_usage
        exit 1
        ;;
esac

exit 0
