#!/bin/bash

# Define variables
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOG_DIR="$PROJECT_ROOT/log"

# Create log directory if it doesn't exist
mkdir -p "$LOG_DIR"

if [ -f "$PROJECT_ROOT/.env" ]; then
  echo "Loading environment variables from .env"
  set -a
  source "$PROJECT_ROOT/.env"
  set +a
fi

echo "=========================================="
echo "Cleaning up existing processes..."
echo "=========================================="
for PORT in 9331 9332 9333; do
  PID=$(lsof -t -i:$PORT)
  if [ -n "$PID" ]; then
    echo "Killing process on port $PORT (PID: $PID)..."
    kill -9 $PID 2>/dev/null || true
  fi
done

NODEMON_PIDS=$(pgrep -f "nodemon.*$PROJECT_ROOT" || true)
if [ -n "$NODEMON_PIDS" ]; then
  echo "Killing existing nodemon processes for $PROJECT_ROOT..."
  pkill -9 -f "nodemon.*$PROJECT_ROOT" 2>/dev/null || true
fi

MAVEN_PIDS=$(pgrep -f "MavenWrapperMain.*$PROJECT_ROOT" || true)
if [ -n "$MAVEN_PIDS" ]; then
  echo "Killing existing Maven wrapper processes for $PROJECT_ROOT..."
  pkill -9 -f "MavenWrapperMain.*$PROJECT_ROOT" 2>/dev/null || true
fi

echo "=========================================="
echo "Starting Loyalty Agent Services"
echo "=========================================="

# 1. Start MCP Server
echo "Starting loyalty-mcp-server..."
cd "$PROJECT_ROOT/loyalty-mcp-server"
../mvnw spring-boot:run -Dmaven.test.skip=true > "$LOG_DIR/loyalty-mcp-server.log" 2>&1 &
MCP_PID=$!
echo "loyalty-mcp-server started with PID: $MCP_PID"

# Wait for MCP Server to start listening on port 9331
echo "Waiting for loyalty-mcp-server to initialize..."
while ! nc -z localhost 9331; do   
  sleep 2
done
echo "loyalty-mcp-server is up!"

# Start nodemon watcher for MCP Server
echo "Starting nodemon watcher for loyalty-mcp-server..."
npx -y nodemon --watch src -e java,xml,yml,yaml,properties --exec "$PROJECT_ROOT/mvnw compile" > "$LOG_DIR/loyalty-mcp-server-nodemon.log" 2>&1 &
MCP_NODEMON_PID=$!


# 2. Start Agent Service
echo "Starting loyalty-agent..."
cd "$PROJECT_ROOT/loyalty-agent"
../mvnw spring-boot:run -Dmaven.test.skip=true > "$LOG_DIR/loyalty-agent.log" 2>&1 &
AGENT_PID=$!
echo "loyalty-agent started with PID: $AGENT_PID"

# Wait for Agent to start listening on port 9332
echo "Waiting for loyalty-agent to initialize..."
while ! nc -z localhost 9332; do   
  sleep 2
done
echo "loyalty-agent is up!"

# Start nodemon watcher for Agent Service
echo "Starting nodemon watcher for loyalty-agent..."
npx -y nodemon --watch src -e java,xml,yml,yaml,properties --exec "$PROJECT_ROOT/mvnw compile" > "$LOG_DIR/loyalty-agent-nodemon.log" 2>&1 &
AGENT_NODEMON_PID=$!


# 3. Start Frontend UI
echo "Starting Frontend UI..."
cd "$PROJECT_ROOT"
pnpm dev > "$LOG_DIR/frontend.log" 2>&1 &
UI_PID=$!
echo "Frontend UI started with PID: $UI_PID"

echo "=========================================="
echo "All services started successfully!"
echo "Log files are stored in: $LOG_DIR"
echo "- $LOG_DIR/loyalty-mcp-server.log"
echo "- $LOG_DIR/loyalty-mcp-server-nodemon.log"
echo "- $LOG_DIR/loyalty-agent.log"
echo "- $LOG_DIR/loyalty-agent-nodemon.log"
echo "- $LOG_DIR/frontend.log"
echo "=========================================="
echo "To stop the services, you can run:"
echo "kill $MCP_PID $MCP_NODEMON_PID $AGENT_PID $AGENT_NODEMON_PID $UI_PID"
