#!/bin/bash
# Foxhunt HFT Trading System - Complete System Startup
# Starts 3 standalone services + databases as per FXT_PLAN.md architecture

set -euo pipefail

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

echo -e "${GREEN}🦊 Starting Foxhunt HFT Trading System${NC}"
echo "===================================================="
echo "Architecture: FXT CLI → 3 Standalone Services → Docker Databases"
echo ""

# Function to check if a port is available
check_port() {
    local port=$1
    if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null; then
        return 1
    else
        return 0
    fi
}

# Function to wait for service to be ready
wait_for_service() {
    local service_name=$1
    local port=$2
    local max_attempts=30
    local attempt=0

    echo -e "${YELLOW}⏳ Waiting for $service_name on port $port...${NC}"
    while [ $attempt -lt $max_attempts ]; do
        if nc -z localhost $port 2>/dev/null; then
            echo -e "${GREEN}✅ $service_name is ready${NC}"
            return 0
        fi
        attempt=$((attempt + 1))
        sleep 2
    done

    echo -e "${RED}❌ $service_name failed to start on port $port${NC}"
    return 1
}

# Step 1: Start Docker databases
echo -e "${BLUE}🗄️  Starting Docker databases...${NC}"
if ! command -v docker-compose >/dev/null 2>&1 && ! command -v docker >/dev/null 2>&1; then
    echo -e "${RED}❌ Docker/docker-compose not found. Please install Docker.${NC}"
    exit 1
fi

# Use docker compose (newer) or docker-compose (older)
DOCKER_COMPOSE_CMD="docker compose"
if ! command -v docker >/dev/null 2>&1 || ! docker compose version >/dev/null 2>&1; then
    DOCKER_COMPOSE_CMD="docker-compose"
fi

echo "Starting databases with $DOCKER_COMPOSE_CMD..."
$DOCKER_COMPOSE_CMD up -d

# Wait for databases to be ready
echo -e "${YELLOW}⏳ Waiting for databases to initialize...${NC}"
sleep 15

# Step 2: Set environment variables for database connections
export DATABASE_URL="postgresql://trading_service:trading_dev_password@localhost:5432/foxhunt"
export BACKTESTING_DATABASE_URL="postgresql://backtesting_service:backtesting_dev_password@localhost:5432/foxhunt_backtesting"
export ML_DATABASE_URL="postgresql://ml_service:ml_dev_password@localhost:5432/foxhunt_ml_training"
export REDIS_URL="redis://localhost:6379"
export INFLUXDB_URL="http://localhost:8086"
export RUST_LOG="info"

# Step 3: Check ports and stop conflicting services
echo -e "${BLUE}🔍 Checking service ports...${NC}"
TRADING_PORT=50051
BACKTESTING_PORT=50052
ML_TRAINING_PORT=50053

# Kill any existing services
pkill -f "trading-service" || true
pkill -f "backtesting-service" || true
pkill -f "ml-training-service" || true
sleep 2

# Step 4: Build and start the 3 standalone services
echo -e "${BLUE}🏗️  Building services...${NC}"
if ! cargo build --release --bin trading-service --bin backtesting-service -p ml_training_service; then
    echo -e "${RED}❌ Failed to build services. Check compilation errors.${NC}"
    exit 1
fi

echo -e "${GREEN}✅ Services built successfully${NC}"

# Step 5: Start services in background
echo -e "${BLUE}🚀 Starting standalone services...${NC}"

# Start Trading Service (port 50051)
echo "📈 Starting Trading Service..."
cargo run --release --bin trading-service &
TRADING_PID=$!
echo $TRADING_PID > .trading-service.pid

# Start Backtesting Service (port 50052)
echo "🔄 Starting Backtesting Service..."
cargo run --release -p backtesting_service &
BACKTESTING_PID=$!
echo $BACKTESTING_PID > .backtesting-service.pid

# Start ML Training Service (port 50053)
echo "🧠 Starting ML Training Service..."
cargo run --release -p ml_training_service &
ML_TRAINING_PID=$!
echo $ML_TRAINING_PID > .ml-training-service.pid

# Step 6: Wait for all services to be ready
echo -e "${YELLOW}⏳ Waiting for services to start...${NC}"
wait_for_service "Trading Service" $TRADING_PORT
wait_for_service "Backtesting Service" $BACKTESTING_PORT
wait_for_service "ML Training Service" $ML_TRAINING_PORT

# Step 7: Display system status
echo -e "${GREEN}🎉 Foxhunt HFT System is running!${NC}"
echo ""
echo "System Architecture:"
echo "├── Trading Service:    localhost:$TRADING_PORT (PID: $TRADING_PID)"
echo "├── Backtesting Service: localhost:$BACKTESTING_PORT (PID: $BACKTESTING_PID)"
echo "├── ML Training Service: localhost:$ML_TRAINING_PORT (PID: $ML_TRAINING_PID)"
echo "└── Databases:          PostgreSQL(5432), InfluxDB(8086), Redis(6379)"
echo ""
echo "To start FXT CLI: cargo run --release -p fxt"
echo "To stop system:      ./stop.sh"
echo "To view logs:        docker logs foxhunt-postgres"
echo ""
echo -e "${GREEN}System ready for FXT connection!${NC}"

# Keep services running and handle shutdown
trap 'echo -e "\n${YELLOW}🛑 Shutting down services...${NC}"; ./stop.sh; exit 0' INT
echo "Press Ctrl+C to stop all services and databases"
wait