version: '3.9'

services:
  # ==========================================================================
  # Neo4j - Knowledge Graph Database (Phase 00 Foundation)
  # ==========================================================================
  neo4j:
    image: neo4j:5.15-community
    container_name: swarmcare-neo4j
    ports:
      - "7474:7474"  # HTTP Browser
      - "7687:7687"  # Bolt Protocol
    environment:
      - NEO4J_AUTH=neo4j/swarmcare2024
      - NEO4J_PLUGINS=["apoc"]
      - NEO4J_dbms_memory_pagecache_size=512M
      - NEO4J_dbms_memory_heap_initial__size=512M
      - NEO4J_dbms_memory_heap_max__size=1G
      - NEO4J_apoc_export_file_enabled=true
      - NEO4J_apoc_import_file_enabled=true
      - NEO4J_apoc_import_file_use__neo4j__config=true
      # Performance tuning for Phase 00 ontologies
      - NEO4J_dbms_query__cache__size=200
      - NEO4J_dbms_logs_query_enabled=INFO
    volumes:
      - neo4j_data:/data
      - neo4j_logs:/logs
      - neo4j_import:/var/lib/neo4j/import
      - ./neo4j/plugins:/plugins
    healthcheck:
      test: ["CMD", "wget", "--spider", "-q", "http://localhost:7474"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s
    networks:
      - swarmcare-network
    restart: unless-stopped

  # ==========================================================================
  # Redis - Caching Layer
  # ==========================================================================
  redis:
    image: redis:7-alpine
    container_name: swarmcare-redis
    ports:
      - "6379:6379"
    command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - swarmcare-network

  # ==========================================================================
  # Phase 00 Ontology Initializer - Loads 13 Medical Ontologies
  # ==========================================================================
  phase00-init:
    build:
      context: ./swarmcare-api
      dockerfile: Dockerfile
    container_name: swarmcare-phase00-init
    environment:
      - NEO4J_URI=bolt://neo4j:7687
      - NEO4J_USER=neo4j
      - NEO4J_PASSWORD=swarmcare2024
      - PYTHONUNBUFFERED=1
    command: ["python", "/app/scripts/init_phase00.py"]
    depends_on:
      neo4j:
        condition: service_healthy
    networks:
      - swarmcare-network
    restart: "no"

  # ==========================================================================
  # SwarmCare API - FastAPI Backend
  # ==========================================================================
  swarmcare-api:
    build:
      context: ./swarmcare-api
      dockerfile: Dockerfile
    container_name: swarmcare-api
    ports:
      - "8001:8001"
    environment:
      - NEO4J_URI=bolt://neo4j:7687
      - NEO4J_USER=neo4j
      - NEO4J_PASSWORD=swarmcare2024
      - REDIS_URL=redis://redis:6379
      - PYTHONUNBUFFERED=1
      - LOG_LEVEL=INFO
      - PHASE00_AUTO_INIT=true
    depends_on:
      neo4j:
        condition: service_healthy
      redis:
        condition: service_healthy
      phase00-init:
        condition: service_completed_successfully
    volumes:
      - ./swarmcare-api:/app
      - ./data:/app/data
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8001/health"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 15s
    networks:
      - swarmcare-network
    restart: unless-stopped

  # ==========================================================================
  # Phase 01 RAG System Initializer - Loads Sample Documents
  # ==========================================================================
  phase01-init:
    build:
      context: ./swarmcare-api
      dockerfile: Dockerfile
    container_name: swarmcare-phase01-init
    environment:
      - API_URL=http://swarmcare-api:8001
      - PYTHONUNBUFFERED=1
    command: ["python", "/app/scripts/init_phase01.py"]
    depends_on:
      swarmcare-api:
        condition: service_healthy
    networks:
      - swarmcare-network
    restart: "no"

  # ==========================================================================
  # Weaviate Vector Database (Optional - for production RAG)
  # Uncomment to use instead of in-memory vector store
  # ==========================================================================
  # weaviate:
  #   image: semitechnologies/weaviate:1.22.4
  #   container_name: swarmcare-weaviate
  #   ports:
  #     - "8080:8080"
  #   environment:
  #     QUERY_DEFAULTS_LIMIT: 25
  #     AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED: 'true'
  #     PERSISTENCE_DATA_PATH: '/var/lib/weaviate'
  #     DEFAULT_VECTORIZER_MODULE: 'none'
  #     ENABLE_MODULES: ''
  #     CLUSTER_HOSTNAME: 'weaviate'
  #   volumes:
  #     - weaviate_data:/var/lib/weaviate
  #   networks:
  #     - swarmcare-network
  #   restart: unless-stopped
  #   healthcheck:
  #     test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/v1/.well-known/ready"]
  #     interval: 30s
  #     timeout: 10s
  #     retries: 3
  #     start_period: 30s

  # ==========================================================================
  # SwarmCare UI - Next.js Frontend
  # ==========================================================================
  swarmcare-ui:
    build:
      context: ./swarmcare-ui
      dockerfile: Dockerfile
    container_name: swarmcare-ui
    ports:
      - "3000:3000"
    environment:
      - NEXT_PUBLIC_API_URL=http://localhost:8001
      - NODE_ENV=production
    depends_on:
      swarmcare-api:
        condition: service_healthy
    networks:
      - swarmcare-network

  # ==========================================================================
  # Prometheus - Metrics Collection
  # ==========================================================================
  prometheus:
    image: prom/prometheus:v2.48.0
    container_name: swarmcare-prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.enable-lifecycle'
    networks:
      - swarmcare-network

  # ==========================================================================
  # Grafana - Visualization
  # ==========================================================================
  grafana:
    image: grafana/grafana:10.2.2
    container_name: swarmcare-grafana
    ports:
      - "3001:3000"
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=swarmcare2024
      - GF_USERS_ALLOW_SIGN_UP=false
    volumes:
      - grafana_data:/var/lib/grafana
      - ./monitoring/grafana/provisioning:/etc/grafana/provisioning
    depends_on:
      - prometheus
    networks:
      - swarmcare-network

networks:
  swarmcare-network:
    driver: bridge

volumes:
  neo4j_data:
  neo4j_logs:
  neo4j_import:
  redis_data:
  prometheus_data:
  grafana_data:
  # Uncomment for Weaviate production deployment
  # weaviate_data:
