version: '3.8'

services:
  # KG Engine API Server
  kg-api:
    build: .
    ports:
      - "8080:8080"
    environment:
      - NEO4J_URI=bolt://neo4j:7687
      - NEO4J_USERNAME=neo4j
      - NEO4J_PASSWORD=kg_engine_password
      - LLM_PROVIDER=${LLM_PROVIDER:-openai}
      - OPENAI_API_KEY=${OPENAI_API_KEY}
      - OPENAI_MODEL=${OPENAI_MODEL:-gpt-4o-mini}
      - OLLAMA_BASE_URL=${OLLAMA_BASE_URL}
      - OLLAMA_MODEL=${OLLAMA_MODEL}
      - LITELLM_BEARER_TOKEN=${LITELLM_BEARER_TOKEN}
      - LITELLM_BASE_URL=${LITELLM_BASE_URL}
      - LITELLM_MODEL=${LITELLM_MODEL}
      - KG_CACHE_TTL=${KG_CACHE_TTL:-300}
      - KG_MAX_BATCH_SIZE=${KG_MAX_BATCH_SIZE:-50}
      - LOG_LEVEL=INFO
    depends_on:
      neo4j:
        condition: service_healthy
    networks:
      - kg_network
    restart: unless-stopped
    volumes:
      - ./logs:/app/logs

  # Neo4j Database
  neo4j:
    image: neo4j:5-community
    ports:
      - "7474:7474"  # HTTP
      - "7687:7687"  # Bolt
    environment:
      - NEO4J_AUTH=neo4j/kg_engine_password
      - NEO4J_PLUGINS=["apoc"]
      - NEO4J_dbms_security_procedures_unrestricted=apoc.*
      - NEO4J_dbms_memory_heap_initial__size=512m
      - NEO4J_dbms_memory_heap_max__size=2G
      - NEO4J_dbms_memory_pagecache_size=1G
    volumes:
      - neo4j_data:/data
      - neo4j_logs:/logs
      - neo4j_plugins:/plugins
    networks:
      - kg_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:7474 || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 40s

networks:
  kg_network:
    driver: bridge

volumes:
  neo4j_data:
    driver: local
  neo4j_logs:
    driver: local
  neo4j_plugins:
    driver: local