version: '3.8'

# BZZZ MCP Integration Docker Compose Configuration
# This configuration deploys the complete MCP-enabled BZZZ system with GPT-4 agents

services:
  # BZZZ P2P Node with MCP Integration
  bzzz-node:
    build:
      context: ..
      dockerfile: Dockerfile
      args:
        - BUILD_TARGET=mcp-enabled
    container_name: bzzz-mcp-node
    networks:
      - bzzz-network
    ports:
      - "8080:8080"   # BZZZ HTTP API
      - "4001:4001"   # LibP2P swarm port
    environment:
      - BZZZ_NODE_ID=${BZZZ_NODE_ID:-bzzz-mcp-1}
      - BZZZ_NETWORK_ID=${BZZZ_NETWORK_ID:-bzzz-local}
      - BZZZ_P2P_PORT=4001
      - BZZZ_HTTP_PORT=8080
      - MCP_ENABLED=true
      - MCP_SERVER_PORT=8081
    volumes:
      - bzzz-data:/var/lib/bzzz
      - ../business/secrets:/secrets:ro
    restart: unless-stopped
    depends_on:
      - redis
      - postgres

  # MCP Server for GPT-4 Integration
  mcp-server:
    build:
      context: ../mcp-server
      dockerfile: Dockerfile
    container_name: bzzz-mcp-server
    networks:
      - bzzz-network
    ports:
      - "8081:8081"   # MCP HTTP API
      - "8082:8082"   # WebSocket endpoint
    environment:
      - NODE_ENV=production
      - BZZZ_NODE_URL=http://bzzz-node:8080
      - BZZZ_NETWORK_ID=${BZZZ_NETWORK_ID:-bzzz-local}
      - OPENAI_API_KEY_FILE=/secrets/openai-api-key-for-bzzz.txt
      - OPENAI_MODEL=${OPENAI_MODEL:-gpt-4}
      - OPENAI_MAX_TOKENS=${OPENAI_MAX_TOKENS:-4000}
      - DAILY_COST_LIMIT=${DAILY_COST_LIMIT:-100.0}
      - MONTHLY_COST_LIMIT=${MONTHLY_COST_LIMIT:-1000.0}
      - MAX_ACTIVE_THREADS=${MAX_ACTIVE_THREADS:-10}
      - MAX_AGENTS=${MAX_AGENTS:-5}
      - LOG_LEVEL=${LOG_LEVEL:-info}
    volumes:
      - ../business/secrets:/secrets:ro
      - mcp-logs:/var/log/mcp
      - mcp-data:/var/lib/mcp
    restart: unless-stopped
    depends_on:
      - bzzz-node
      - postgres
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8081/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Agent Manager Service
  agent-manager:
    build:
      context: ..
      dockerfile: deploy/Dockerfile.agent-manager
    container_name: bzzz-agent-manager
    networks:
      - bzzz-network
    environment:
      - MCP_SERVER_URL=http://mcp-server:8081
      - POSTGRES_URL=postgres://bzzz:${POSTGRES_PASSWORD}@postgres:5432/bzzz_mcp
      - REDIS_URL=redis://redis:6379
      - AGENT_LIFECYCLE_INTERVAL=30s
      - AGENT_HEALTH_CHECK_INTERVAL=60s
      - COST_MONITORING_INTERVAL=300s
    volumes:
      - agent-data:/var/lib/agents
      - ../business/secrets:/secrets:ro
    restart: unless-stopped
    depends_on:
      - mcp-server
      - postgres
      - redis

  # Conversation Manager Service  
  conversation-manager:
    build:
      context: ..
      dockerfile: deploy/Dockerfile.conversation-manager
    container_name: bzzz-conversation-manager
    networks:
      - bzzz-network
    environment:
      - MCP_SERVER_URL=http://mcp-server:8081
      - POSTGRES_URL=postgres://bzzz:${POSTGRES_PASSWORD}@postgres:5432/bzzz_mcp
      - REDIS_URL=redis://redis:6379
      - THREAD_CLEANUP_INTERVAL=1h
      - ESCALATION_CHECK_INTERVAL=5m
      - SUMMARY_GENERATION_INTERVAL=15m
    volumes:
      - conversation-data:/var/lib/conversations
    restart: unless-stopped
    depends_on:
      - mcp-server
      - postgres
      - redis

  # Cost Tracker Service
  cost-tracker:
    build:
      context: ..
      dockerfile: deploy/Dockerfile.cost-tracker
    container_name: bzzz-cost-tracker
    networks:
      - bzzz-network
    environment:
      - MCP_SERVER_URL=http://mcp-server:8081
      - POSTGRES_URL=postgres://bzzz:${POSTGRES_PASSWORD}@postgres:5432/bzzz_mcp
      - OPENAI_API_KEY_FILE=/secrets/openai-api-key-for-bzzz.txt
      - COST_CALCULATION_INTERVAL=5m
      - ALERT_WEBHOOK_URL=${ALERT_WEBHOOK_URL}
      - SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL}
    volumes:
      - cost-data:/var/lib/costs
      - ../business/secrets:/secrets:ro
    restart: unless-stopped
    depends_on:
      - mcp-server
      - postgres

  # PostgreSQL Database for MCP data
  postgres:
    image: postgres:15-alpine
    container_name: bzzz-mcp-postgres
    networks:
      - bzzz-network
    environment:
      - POSTGRES_DB=bzzz_mcp
      - POSTGRES_USER=bzzz
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./init-db.sql:/docker-entrypoint-initdb.d/init.sql
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U bzzz -d bzzz_mcp"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Redis for caching and session management
  redis:
    image: redis:7-alpine
    container_name: bzzz-mcp-redis
    networks:
      - bzzz-network
    command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
    volumes:
      - redis-data:/data
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

  # WHOOSH Integration Service
  whoosh-integration:
    build:
      context: ../../../WHOOSH
      dockerfile: Dockerfile
    container_name: bzzz-whoosh-integration
    networks:
      - bzzz-network
      - whoosh-network
    environment:
      - WHOOSH_API_URL=${WHOOSH_API_URL}
      - WHOOSH_API_KEY=${WHOOSH_API_KEY}
      - MCP_SERVER_URL=http://mcp-server:8081
      - INTEGRATION_SYNC_INTERVAL=5m
    volumes:
      - whoosh-integration-data:/var/lib/whoosh-integration
      - ../business/secrets:/secrets:ro
    restart: unless-stopped
    depends_on:
      - mcp-server

  # SLURP Integration Service (Context Curation)
  slurp-integration:
    build:
      context: ../../../slurp
      dockerfile: Dockerfile
    container_name: bzzz-slurp-integration
    networks:
      - bzzz-network
      - slurp-network
    environment:
      - SLURP_API_URL=${SLURP_API_URL}
      - SLURP_API_KEY=${SLURP_API_KEY}
      - MCP_SERVER_URL=http://mcp-server:8081
      - CONTEXT_SYNC_INTERVAL=2m
      - RELEVANCE_THRESHOLD=0.7
    volumes:
      - slurp-integration-data:/var/lib/slurp-integration
      - ../business/secrets:/secrets:ro
    restart: unless-stopped
    depends_on:
      - mcp-server

  # Monitoring and Observability
  prometheus:
    image: prom/prometheus:latest
    container_name: bzzz-mcp-prometheus
    networks:
      - bzzz-network
    ports:
      - "9090:9090"
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--storage.tsdb.retention.time=200h'
      - '--web.enable-lifecycle'
    volumes:
      - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus-data:/prometheus
    restart: unless-stopped

  grafana:
    image: grafana/grafana:latest
    container_name: bzzz-mcp-grafana
    networks:
      - bzzz-network
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_USER=${GRAFANA_USER:-admin}
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD}
      - GF_USERS_ALLOW_SIGN_UP=false
    volumes:
      - grafana-data:/var/lib/grafana
      - ./monitoring/grafana/dashboards:/var/lib/grafana/dashboards
      - ./monitoring/grafana/provisioning:/etc/grafana/provisioning
    restart: unless-stopped
    depends_on:
      - prometheus

  # Log Aggregation
  loki:
    image: grafana/loki:latest
    container_name: bzzz-mcp-loki
    networks:
      - bzzz-network
    ports:
      - "3100:3100"
    command: -config.file=/etc/loki/local-config.yaml
    volumes:
      - loki-data:/loki
    restart: unless-stopped

  promtail:
    image: grafana/promtail:latest
    container_name: bzzz-mcp-promtail
    networks:
      - bzzz-network
    volumes:
      - ./monitoring/promtail-config.yml:/etc/promtail/config.yml
      - /var/log:/var/log:ro
      - /var/lib/docker/containers:/var/lib/docker/containers:ro
    command: -config.file=/etc/promtail/config.yml
    restart: unless-stopped
    depends_on:
      - loki

networks:
  bzzz-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16
  whoosh-network:
    external: true
  slurp-network:
    external: true

volumes:
  bzzz-data:
    driver: local
  mcp-logs:
    driver: local
  mcp-data:
    driver: local
  agent-data:
    driver: local
  conversation-data:
    driver: local
  cost-data:
    driver: local
  postgres-data:
    driver: local
  redis-data:
    driver: local
  whoosh-integration-data:
    driver: local
  slurp-integration-data:
    driver: local
  prometheus-data:
    driver: local
  grafana-data:
    driver: local
  loki-data:
    driver: local