#!/bin/bash

# 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 "${BLUE}🚀 Starting Automated Development Pipeline - Phase 1${NC}"
echo "=================================================="

# Check if we're in the right directory
if [ ! -f "docker-compose.yml" ]; then
    echo -e "${RED}❌ Error: docker-compose.yml not found. Please run from project root directory.${NC}"
    exit 1
fi

# Check if Docker is running
if ! docker info > /dev/null 2>&1; then
    echo -e "${RED}❌ Docker is not running. Please start Docker Desktop first.${NC}"
    exit 1
fi

# Check if docker compose is available (try both modern and legacy)
if command -v "docker" &> /dev/null && docker compose version &> /dev/null; then
    DOCKER_COMPOSE="docker compose"
    echo -e "${GREEN}✅ Using modern Docker Compose${NC}"
elif command -v docker-compose &> /dev/null; then
    DOCKER_COMPOSE="docker-compose"
    echo -e "${YELLOW}⚠️  Using legacy docker-compose${NC}"
else
    echo -e "${RED}❌ Docker Compose is not available. Please install Docker Compose.${NC}"
    exit 1
fi

# Create necessary directories
echo -e "${BLUE}📁 Creating necessary directories...${NC}"
mkdir -p logs generated_projects
mkdir -p services/{requirement-processor,tech-stack-selector,architecture-designer,code-generator,test-generator,deployment-manager}/logs
touch generated_projects/.gitkeep

# Load environment variables
if [ -f .env ]; then
    echo -e "${BLUE}📋 Loading environment variables...${NC}"
    export $(cat .env | grep -v '^#' | grep -v '^$' | xargs)
else
    echo -e "${YELLOW}⚠️  .env file not found. Using default values.${NC}"
fi

# Clean up any existing containers
echo -e "${BLUE}🧹 Cleaning up existing containers...${NC}"
$DOCKER_COMPOSE down > /dev/null 2>&1

# Remove orphaned containers
$DOCKER_COMPOSE down --remove-orphans > /dev/null 2>&1

# Pull/build required images
echo -e "${BLUE}📥 Building and pulling Docker images...${NC}"
$DOCKER_COMPOSE build --no-cache rabbitmq
$DOCKER_COMPOSE pull postgres redis mongodb

echo -e "${BLUE}🔄 Starting core infrastructure services...${NC}"
$DOCKER_COMPOSE up -d postgres redis mongodb rabbitmq

# Function to check service health
check_service_health() {
    local service_name=$1
    local check_command=$2
    local max_attempts=30
    local attempt=1
    
    echo -n -e "${BLUE}⏳ Waiting for $service_name to be ready${NC}"
    
    while [ $attempt -le $max_attempts ]; do
        if eval "$check_command" > /dev/null 2>&1; then
            echo -e " ${GREEN}✅${NC}"
            return 0
        fi
        echo -n "."
        sleep 2
        ((attempt++))
    done
    
    echo -e " ${RED}❌ Failed after $max_attempts attempts${NC}"
    return 1
}

# Wait for services to be ready with individual health checks
echo -e "${BLUE}⏳ Waiting for infrastructure services to be ready...${NC}"

# PostgreSQL health check
check_service_health "PostgreSQL" "$DOCKER_COMPOSE exec -T postgres pg_isready -U pipeline_admin -d dev_pipeline"

# Redis health check  
check_service_health "Redis" "$DOCKER_COMPOSE exec -T redis redis-cli -a redis_secure_2024 ping | grep -q PONG"

# MongoDB health check
check_service_health "MongoDB" "$DOCKER_COMPOSE exec -T mongodb mongosh --eval 'db.runCommand(\"ping\")' --quiet"

# RabbitMQ health check (needs more time)
check_service_health "RabbitMQ" "$DOCKER_COMPOSE exec -T rabbitmq rabbitmq-diagnostics ping"

echo ""
echo -e "${BLUE}🔍 Running comprehensive service health checks...${NC}"

# Detailed health check function
detailed_health_check() {
    local service=$1
    local check_cmd=$2
    local port=$3
    
    echo -n -e "${BLUE}🔍 $service:${NC} "
    
    # Check if container is running
    if ! $DOCKER_COMPOSE ps $service | grep -q "Up"; then
        echo -e "${RED}❌ Container not running${NC}"
        return 1
    fi
    
    # Check if port is accessible
    if [ ! -z "$port" ]; then
        if ! nc -z localhost $port 2>/dev/null; then
            echo -e "${YELLOW}⚠️  Port $port not accessible${NC}"
            return 1
        fi
    fi
    
    # Run health check command
    if eval "$check_cmd" > /dev/null 2>&1; then
        echo -e "${GREEN}✅ Healthy${NC}"
        return 0
    else
        echo -e "${RED}❌ Health check failed${NC}"
        echo -e "   ${YELLOW}Checking logs:${NC}"
        $DOCKER_COMPOSE logs --tail=5 $service | sed 's/^/   /'
        return 1
    fi
}

# Run detailed health checks
detailed_health_check "postgres" "$DOCKER_COMPOSE exec -T postgres pg_isready -U pipeline_admin -d dev_pipeline" "5432"
detailed_health_check "redis" "$DOCKER_COMPOSE exec -T redis redis-cli -a redis_secure_2024 ping | grep -q PONG" "6379"
detailed_health_check "mongodb" "$DOCKER_COMPOSE exec -T mongodb mongosh --eval 'db.runCommand(\"ping\").ok' --quiet" "27017"
detailed_health_check "rabbitmq" "$DOCKER_COMPOSE exec -T rabbitmq rabbitmq-diagnostics ping" "15672"

echo ""
echo -e "${BLUE}🧪 Running database initialization tests...${NC}"

# Test database connections
echo -n -e "${BLUE}📊 Testing PostgreSQL connection:${NC} "
if $DOCKER_COMPOSE exec -T postgres psql -U pipeline_admin -d dev_pipeline -c "SELECT version();" > /dev/null 2>&1; then
    echo -e "${GREEN}✅ Connected${NC}"
else
    echo -e "${RED}❌ Connection failed${NC}"
fi

echo -n -e "${BLUE}📊 Testing Redis connection:${NC} "
if $DOCKER_COMPOSE exec -T redis redis-cli -a redis_secure_2024 ping 2>/dev/null | grep -q PONG; then
    echo -e "${GREEN}✅ Connected${NC}"
else
    echo -e "${RED}❌ Connection failed${NC}"
fi

echo -n -e "${BLUE}📊 Testing MongoDB connection:${NC} "
if $DOCKER_COMPOSE exec -T mongodb mongosh --eval "db.runCommand('ping')" --quiet > /dev/null 2>&1; then
    echo -e "${GREEN}✅ Connected${NC}"
else
    echo -e "${RED}❌ Connection failed${NC}"
fi

# Test RabbitMQ management interface
echo -n -e "${BLUE}📊 Testing RabbitMQ management:${NC} "
if curl -s -u pipeline_admin:rabbit_secure_2024 http://localhost:15672/api/overview > /dev/null 2>&1; then
    echo -e "${GREEN}✅ Management UI accessible${NC}"
else
    echo -e "${YELLOW}⚠️  Management UI not ready yet${NC}"
fi

echo ""
echo -e "${BLUE}📊 Infrastructure Status Summary:${NC}"
echo "============================================"

# Show container status
echo -e "${BLUE}🐳 Container Status:${NC}"
$DOCKER_COMPOSE ps --format "table {{.Service}}\t{{.State}}\t{{.Status}}"

echo ""
echo -e "${BLUE}💾 Volume Usage:${NC}"
docker system df --format "table {{.Type}}\t{{.TotalCount}}\t{{.Size}}\t{{.Reclaimable}}"

echo ""
echo -e "${BLUE}🌐 Service URLs:${NC}"
echo "  📊 RabbitMQ Management: http://localhost:15672"
echo "     Username: pipeline_admin"
echo "     Password: rabbit_secure_2024"
echo "  🐘 PostgreSQL: localhost:5432"
echo "  🔴 Redis: localhost:6379"
echo "  🍃 MongoDB: localhost:27017"

echo ""
echo -e "${BLUE}�� Quick Connection Tests:${NC}"
echo "  PostgreSQL: $DOCKER_COMPOSE exec postgres psql -U pipeline_admin -d dev_pipeline -c 'SELECT version();'"
echo "  Redis: $DOCKER_COMPOSE exec redis redis-cli -a redis_secure_2024 ping"
echo "  MongoDB: $DOCKER_COMPOSE exec mongodb mongosh --eval 'db.runCommand(\"ping\")'"
echo "  RabbitMQ Queues: python3 scripts/rabbitmq/test-queues.py"

echo ""
echo -e "${GREEN}✅ Phase 1 Foundation Infrastructure is running!${NC}"
echo ""
echo -e "${BLUE}📝 Next Steps:${NC}"
echo "  1. Test database connections using the commands above"
echo "  2. Check RabbitMQ management UI at http://localhost:15672"
echo "  3. Review logs with: $DOCKER_COMPOSE logs [service-name]"
echo "  4. Stop services with: ./scripts/setup/stop.sh"
echo "  5. Check status with: ./scripts/setup/status.sh"
echo ""
echo -e "${YELLOW}🎯 Ready to proceed to Phase 2: AI Services Integration!${NC}"
