#!/bin/bash

# Trading Bot Docker Management Script

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

# Default options
ACTION=""
SERVICE=""
DEV=false

show_help() {
    echo ""
    echo -e "${GREEN}🤖 Trading Bot Docker Management${NC}"
    echo ""
    echo -e "${CYAN}Usage: $0 <action> [options]${NC}"
    echo ""
    echo -e "${YELLOW}Actions:${NC}"
    echo "  start      Start infrastructure services"
    echo "  stop       Stop infrastructure services"  
    echo "  restart    Restart infrastructure services"
    echo "  status     Show service status"
    echo "  logs       Show service logs"
    echo "  reset      Reset all data (destructive)"
    echo "  admin      Start admin interfaces"
    echo "  monitoring Start monitoring stack"
    echo "  help       Show this help"
    echo ""
    echo -e "${YELLOW}Options:${NC}"
    echo "  --service <name>  Specify a specific service"
    echo "  --dev             Use development configuration"
    echo ""
    echo -e "${CYAN}Examples:${NC}"
    echo "  $0 start"
    echo "  $0 start --dev"
    echo "  $0 logs --service dragonfly"
    echo "  $0 admin"
}

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        start|stop|restart|status|logs|reset|admin|monitoring|help)
            ACTION="$1"
            shift
            ;;
        --service|-s)
            SERVICE="$2"
            shift 2
            ;;
        --dev|-d)
            DEV=true
            shift
            ;;
        --help|-h)
            show_help
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            echo "Use --help for usage information"
            exit 1
            ;;
    esac
done

if [ -z "$ACTION" ]; then
    echo "Error: Action is required"
    show_help
    exit 1
fi

# Set up compose files
if [ "$DEV" = true ]; then
    COMPOSE_FILES="-f docker-compose.yml -f docker-compose.dev.yml"
else
    COMPOSE_FILES="-f docker-compose.yml"
fi

case "$ACTION" in
    "start")
        echo -e "${GREEN}🚀 Starting Trading Bot infrastructure...${NC}"
        if [ -n "$SERVICE" ]; then
            docker-compose $COMPOSE_FILES up -d "$SERVICE"
        else
            docker-compose $COMPOSE_FILES up -d dragonfly postgres questdb
        fi
        echo -e "${GREEN}✅ Infrastructure started!${NC}"
        echo ""
        echo -e "${CYAN}🔗 Access Points:${NC}"
        echo "   Dragonfly: localhost:6379"
        echo "   PostgreSQL: localhost:5432"
        echo "   QuestDB Console: http://localhost:9000"
        echo ""
        echo -e "${BLUE}💡 Use '$0 admin' to start admin interfaces${NC}"
        ;;
        
    "stop")
        echo -e "${YELLOW}🛑 Stopping Trading Bot infrastructure...${NC}"
        if [ -n "$SERVICE" ]; then
            docker-compose $COMPOSE_FILES stop "$SERVICE"
        else
            docker-compose $COMPOSE_FILES down
        fi
        echo -e "${GREEN}✅ Infrastructure stopped!${NC}"
        ;;
        
    "restart")
        echo -e "${YELLOW}🔄 Restarting Trading Bot infrastructure...${NC}"
        if [ -n "$SERVICE" ]; then
            docker-compose $COMPOSE_FILES restart "$SERVICE"
        else
            docker-compose $COMPOSE_FILES restart
        fi
        echo -e "${GREEN}✅ Infrastructure restarted!${NC}"
        ;;
        
    "status")
        echo -e "${CYAN}📊 Trading Bot Infrastructure Status:${NC}"
        docker-compose $COMPOSE_FILES ps
        echo ""
        echo -e "${CYAN}🔍 Health Checks:${NC}"
        docker ps --filter "name=trading-bot" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
        ;;
        
    "logs")
        if [ -n "$SERVICE" ]; then
            echo -e "${CYAN}📋 Logs for $SERVICE:${NC}"
            docker-compose $COMPOSE_FILES logs -f "$SERVICE"
        else
            echo -e "${CYAN}📋 All service logs:${NC}"
            docker-compose $COMPOSE_FILES logs -f
        fi
        ;;
        
    "reset")
        echo -e "${RED}⚠️  Resetting Trading Bot infrastructure (will delete all data)...${NC}"
        read -p "Are you sure? Type 'yes' to confirm: " confirm
        if [ "$confirm" = "yes" ]; then
            docker-compose $COMPOSE_FILES down -v
            echo -e "${YELLOW}🗑️  Volumes removed${NC}"
            docker-compose $COMPOSE_FILES up -d dragonfly postgres questdb
            echo -e "${GREEN}✅ Infrastructure reset complete!${NC}"
        else
            echo -e "${YELLOW}❌ Reset cancelled${NC}"
        fi
        ;;
        
    "admin")
        echo -e "${GREEN}🔧 Starting admin interfaces...${NC}"
        docker-compose $COMPOSE_FILES up -d redis-insight pgadmin
        echo -e "${GREEN}✅ Admin interfaces started!${NC}"
        echo ""
        echo -e "${CYAN}🔗 Admin Access:${NC}"
        echo "   Redis Insight: http://localhost:8001"
        echo "   PgAdmin: http://localhost:8080"
        echo "     Email: admin@tradingbot.local"
        echo "     Password: admin123"
        ;;
        
    "monitoring")
        echo -e "${GREEN}📊 Starting monitoring stack...${NC}"
        docker-compose $COMPOSE_FILES up -d prometheus grafana loki
        echo -e "${GREEN}✅ Monitoring started!${NC}"
        echo ""
        echo -e "${CYAN}🔗 Monitoring Access:${NC}"
        echo "   Prometheus: http://localhost:9090"
        echo "   Grafana: http://localhost:3000"
        echo "     Username: admin"
        echo "     Password: admin"
        echo "   Loki: http://localhost:3100"
        ;;
        
    "help")
        show_help
        ;;
        
    *)
        echo "Unknown action: $ACTION"
        show_help
        exit 1
        ;;
esac
