#!/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}🛠️  Development Helper Script${NC}"
echo "============================="

show_help() {
    echo -e "${BLUE}Available commands:${NC}"
    echo "  db-shell [postgres|mongo|redis] - Connect to database shell"
    echo "  test-db                          - Test all database connections"
    echo "  test-rabbitmq                    - Test RabbitMQ queues"
    echo "  reset-db                         - Reset all databases"
    echo "  quick-start                      - Start only essential services"
    echo "  health                           - Comprehensive health check"
    echo ""
}

case $1 in
    "db-shell")
        case $2 in
            "postgres")
                echo -e "${BLUE}🐘 Connecting to PostgreSQL...${NC}"
                docker-compose exec postgres psql -U pipeline_admin -d dev_pipeline
                ;;
            "mongo")
                echo -e "${BLUE}🍃 Connecting to MongoDB...${NC}"
                docker-compose exec mongodb mongosh
                ;;
            "redis")
                echo -e "${BLUE}🔴 Connecting to Redis...${NC}"
                docker-compose exec redis redis-cli
                ;;
            *)
                echo -e "${RED}❌ Please specify: postgres, mongo, or redis${NC}"
                ;;
        esac
        ;;
    "test-db")
        echo -e "${BLUE}🧪 Testing database connections...${NC}"
        echo -n "PostgreSQL: "
        if docker-compose exec -T postgres psql -U pipeline_admin -d dev_pipeline -c "SELECT version();" > /dev/null 2>&1; then
            echo -e "${GREEN}✅${NC}"
        else
            echo -e "${RED}❌${NC}"
        fi
        
        echo -n "MongoDB: "
        if docker-compose exec -T mongodb mongosh --eval "db.runCommand('ping')" --quiet > /dev/null 2>&1; then
            echo -e "${GREEN}✅${NC}"
        else
            echo -e "${RED}❌${NC}"
        fi
        
        echo -n "Redis: "
        if docker-compose exec -T redis redis-cli ping | grep -q PONG; then
            echo -e "${GREEN}✅${NC}"
        else
            echo -e "${RED}❌${NC}"
        fi
        ;;
    "test-rabbitmq")
        echo -e "${BLUE}🧪 Testing RabbitMQ...${NC}"
        if [ -f "scripts/rabbitmq/test-queues.py" ]; then
            python3 scripts/rabbitmq/test-queues.py
        else
            echo -e "${RED}❌ RabbitMQ test script not found${NC}"
        fi
        ;;
    "reset-db")
        echo -e "${YELLOW}⚠️  This will reset ALL databases!${NC}"
        read -p "Are you sure? (y/N): " -n 1 -r
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            echo -e "${BLUE}🔄 Resetting databases...${NC}"
            docker-compose down
            docker volume rm $(docker volume ls -q | grep pipeline) 2>/dev/null || true
            docker-compose up -d postgres redis mongodb rabbitmq
            echo -e "${GREEN}✅ Databases reset${NC}"
        fi
        ;;
    "quick-start")
        echo -e "${BLUE}🚀 Quick start - essential services only...${NC}"
        docker-compose up -d postgres redis mongodb rabbitmq
        ;;
    "health")
        echo -e "${BLUE}🏥 Comprehensive health check...${NC}"
        ./scripts/setup/status.sh
        ;;
    *)
        show_help
        ;;
esac
