#!/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}📊 Automated Development Pipeline Status${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

# Detect Docker Compose command
if command -v "docker" &> /dev/null && docker compose version &> /dev/null; then
    DOCKER_COMPOSE="docker compose"
elif command -v docker-compose &> /dev/null; then
    DOCKER_COMPOSE="docker-compose"
else
    echo -e "${RED}❌ Docker Compose is not available.${NC}"
    exit 1
fi

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

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

echo ""
echo -e "${BLUE}🔍 Service Health Checks:${NC}"
echo "-------------------------"

# Define services to check
services=("postgres" "redis" "mongodb" "rabbitmq")
ports=(5432 6379 27017 15672)
health_commands=(
    "$DOCKER_COMPOSE exec -T postgres pg_isready -U pipeline_admin -d dev_pipeline"
    "$DOCKER_COMPOSE exec -T redis redis-cli ping"
    "$DOCKER_COMPOSE exec -T mongodb mongosh --eval 'db.runCommand(\"ping\").ok' --quiet"
    "$DOCKER_COMPOSE exec -T rabbitmq rabbitmq-diagnostics ping"
)

for i in "${!services[@]}"; do
    service=${services[$i]}
    port=${ports[$i]}
    health_cmd=${health_commands[$i]}
    
    echo -n -e "${BLUE}$service:${NC} "
    
    # Check if container is running
    if $DOCKER_COMPOSE ps $service | grep -q "Up"; then
        echo -n -e "${GREEN}Running${NC}"
        
        # Check port accessibility
        if nc -z localhost $port 2>/dev/null; then
            echo -n -e " | ${GREEN}Port $port Open${NC}"
        else
            echo -n -e " | ${RED}Port $port Closed${NC}"
        fi
        
        # Check health
        if eval "$health_cmd" > /dev/null 2>&1; then
            echo -e " | ${GREEN}Healthy${NC}"
        else
            echo -e " | ${RED}Unhealthy${NC}"
        fi
    else
        echo -e "${RED}Stopped${NC}"
    fi
done

echo ""
echo -e "${BLUE}�� Service Endpoints:${NC}"
echo "--------------------"
echo "  🐘 PostgreSQL: localhost:5432"
echo "  🔴 Redis: localhost:6379"
echo "  🍃 MongoDB: localhost:27017"
echo "  🐰 RabbitMQ AMQP: localhost:5672"
echo "  📊 RabbitMQ Management: http://localhost:15672"

echo ""
echo -e "${BLUE}🔗 Quick Connection Tests:${NC}"
echo "--------------------------"

# PostgreSQL test
echo -n -e "${BLUE}PostgreSQL:${NC} "
if $DOCKER_COMPOSE exec -T postgres psql -U pipeline_admin -d dev_pipeline -c "SELECT 1;" > /dev/null 2>&1; then
    echo -e "${GREEN}✅ Connection successful${NC}"
else
    echo -e "${RED}❌ Connection failed${NC}"
fi

# Redis test
echo -n -e "${BLUE}Redis:${NC} "
redis_response=$($DOCKER_COMPOSE exec -T redis redis-cli ping 2>/dev/null)
if [[ "$redis_response" == *"PONG"* ]]; then
    echo -e "${GREEN}✅ Connection successful${NC}"
else
    echo -e "${RED}❌ Connection failed${NC}"
fi

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

# RabbitMQ test
echo -n -e "${BLUE}RabbitMQ:${NC} "
if $DOCKER_COMPOSE exec -T rabbitmq rabbitmq-diagnostics ping > /dev/null 2>&1; then
    echo -e "${GREEN}✅ Connection successful${NC}"
else
    echo -e "${RED}❌ Connection failed${NC}"
fi

# RabbitMQ Management UI test
echo -n -e "${BLUE}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 "${RED}❌ Management UI not accessible${NC}"
fi

echo ""
echo -e "${BLUE}🔧 Management Commands:${NC}"
echo "----------------------"
echo "  🚀 Start services: ./scripts/setup/start.sh"
echo "  🛑 Stop services: ./scripts/setup/stop.sh"
echo "  📋 View logs: $DOCKER_COMPOSE logs [service-name]"
echo "  🔄 Restart service: $DOCKER_COMPOSE restart [service-name]"
echo "  🧪 Test RabbitMQ: python3 scripts/rabbitmq/test-queues.py"
echo ""
