services:  # Dragonfly - Redis replacement for caching and events
  dragonfly:
    image: docker.dragonflydb.io/dragonflydb/dragonfly:latest
    container_name: trading-bot-dragonfly
    ports:
      - "6379:6379"
    deploy:
      resources:
        limits:
          cpus: '4.0'      # Use up to 4 CPU cores
          memory: 8G       # Use up to 8GB RAM
        reservations:
          cpus: '2.0'      # Reserve at least 2 CPU cores
          memory: 4G       # Reserve at least 4GB RAM
    command:
      - dragonfly
      - --logtostderr
      - --maxmemory=8gb
      - --proactor_threads=8
      - --bind=0.0.0.0
      - --admin_port=6380
      - --cluster_mode=emulated
      - --hz=500
      - --tcp_keepalive=60
      - --conn_use_incoming_cpu=true
      - --lock_on_hashtags
      - --default_lua_flags=allow-undeclared-keys
    volumes:
      - dragonfly_data:/data
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3
    networks:
      - trading-bot-network

  # PostgreSQL - Operational data (orders, positions, strategies)
  postgres:
    image: postgres:16-alpine
    container_name: trading-bot-postgres
    environment:
      POSTGRES_DB: trading_bot
      POSTGRES_USER: trading_user
      POSTGRES_PASSWORD: trading_pass_dev
      POSTGRES_INITDB_ARGS: "--encoding=UTF-8"
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./database/postgres/init:/docker-entrypoint-initdb.d
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U trading_user -d trading_bot"]
      interval: 30s
      timeout: 10s
      retries: 3
    networks:
      - trading-bot-network

  # QuestDB - Time-series data (OHLCV, indicators, performance)
  questdb:
    image: questdb/questdb:latest
    container_name: trading-bot-questdb
    ports:
      - "9000:9000"   # Web console
      - "8812:8812"   # PostgreSQL wire protocol
      - "9009:9009"   # InfluxDB line protocol
    volumes:
      - questdb_data:/var/lib/questdb
    environment:
      - QDB_TELEMETRY_ENABLED=false
    restart: unless-stopped
    networks:
      - trading-bot-network

  # MongoDB - Document storage (sentiment, raw docs, unstructured data)
  mongodb:
    image: mongo:7-jammy
    container_name: trading-bot-mongodb
    environment:
      MONGO_INITDB_ROOT_USERNAME: trading_admin
      MONGO_INITDB_ROOT_PASSWORD: trading_mongo_dev
      MONGO_INITDB_DATABASE: stock
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data:/data/db
      - ./database/mongodb/init:/docker-entrypoint-initdb.d
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
      interval: 30s
      timeout: 10s
      retries: 3
    networks:
      - trading-bot-network  # Redis Insight - GUI for Dragonfly debugging
  redis-insight:
    image: redislabs/redisinsight:latest
    container_name: trading-bot-redis-insight
    ports:
      - "8001:5540"
    depends_on:
      - dragonfly
    restart: unless-stopped
    networks:
      - trading-bot-network

  # PgAdmin - PostgreSQL GUI
  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: trading-bot-pgadmin
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@tradingbot.local
      PGADMIN_DEFAULT_PASSWORD: admin123
      PGADMIN_CONFIG_SERVER_MODE: 'False'
      PGADMIN_DISABLE_POSTFIX: 'true'
    ports:
      - "8080:80"
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    depends_on:
      - postgres
    restart: unless-stopped
    networks:
      - trading-bot-network

  # Mongo Express - MongoDB GUI
  mongo-express:
    image: mongo-express:latest
    container_name: trading-bot-mongo-express
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: trading_admin
      ME_CONFIG_MONGODB_ADMINPASSWORD: trading_mongo_dev
      ME_CONFIG_MONGODB_SERVER: mongodb
      ME_CONFIG_MONGODB_PORT: 27017
      ME_CONFIG_BASICAUTH_USERNAME: admin
      ME_CONFIG_BASICAUTH_PASSWORD: admin123
    ports:
      - "8081:8081"
    depends_on:
      - mongodb
    restart: unless-stopped
    networks:
      - trading-bot-network

  # Prometheus - Metrics collection
  prometheus:
    image: prom/prometheus:latest
    container_name: trading-bot-prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./monitoring/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--web.enable-lifecycle'
    restart: unless-stopped
    networks:
      - trading-bot-network

  # Loki - Log aggregation
  loki:
    image: grafana/loki:2.9.2
    container_name: trading-bot-loki
    ports:
      - "3100:3100"
    volumes:
      - loki_data:/loki
      - ./monitoring/loki:/etc/loki
    command: -config.file=/etc/loki/loki-config.yaml
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:3100/ready"]
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped
    networks:
      - trading-bot-network
  # Grafana - Visualization for logs and metrics
  grafana:
    image: grafana/grafana:10.2.0
    container_name: trading-bot-grafana
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin
      - GF_SECURITY_ADMIN_USER=admin
      - GF_PATHS_PROVISIONING=/etc/grafana/provisioning
      - GF_USERS_ALLOW_SIGN_UP=false
    volumes:
      - grafana_data:/var/lib/grafana
      - ./monitoring/grafana/provisioning:/etc/grafana/provisioning
    depends_on:
      - prometheus
      - loki
    restart: unless-stopped
    networks:
      - trading-bot-network

  # Bull Board - Queue monitoring
  bull-board:
    image: venatum/bull-board:latest
    container_name: trading-bot-bull-board
    ports:
      - "3001:3000"
    environment:
      - REDIS_HOST=dragonfly
      - REDIS_PORT=6379
      - REDIS_PASSWORD=
      - REDIS_DB=1
      - REDIS_URL=redis://dragonfly:6379
    depends_on:
      - dragonfly
    restart: unless-stopped
    networks:
      - trading-bot-network

volumes:
  postgres_data:
  questdb_data:
  dragonfly_data:
  mongodb_data:
  pgadmin_data:
  prometheus_data:
  grafana_data:
  loki_data:

networks:
  trading-bot-network:
    driver: bridge
