# txt2kg Docker Compose - ArangoDB + Ollama (Default)
#
# Default stack tested and working on DGX Spark
#
# Usage:
#   ./start.sh                    # Default: ArangoDB + Ollama
#   ./start.sh --vector-search    # Add Qdrant + Sentence Transformers
#
# For Neo4j + vLLM, use: ./start.sh --vllm

services:
  app:
    build:
      context: ../..
      dockerfile: deploy/app/Dockerfile
    ports:
      - '3001:3000'
    environment:
      # ArangoDB configuration
      - ARANGODB_URL=http://arangodb:8529
      - ARANGODB_DB=txt2kg
      - GRAPH_DB_TYPE=arangodb
      # Disable Neo4j
      - NEO4J_URI=bolt://localhost:7687
      - NEO4J_USER=neo4j
      - NEO4J_PASSWORD=password123
      # Ollama configuration
      - OLLAMA_BASE_URL=http://ollama:11434/v1
      - OLLAMA_MODEL=llama3.1:8b
      # vLLM disabled in default Ollama mode
      # - VLLM_BASE_URL=http://localhost:8001/v1
      - VLLM_MODEL=disabled
      # Vector DB configuration
      - QDRANT_URL=http://qdrant:6333
      - VECTOR_DB_TYPE=qdrant
      # Embeddings configuration
      - LANGCHAIN_TRACING_V2=true
      - SENTENCE_TRANSFORMER_URL=http://sentence-transformers:80
      - MODEL_NAME=all-MiniLM-L6-v2
      - EMBEDDINGS_API_URL=http://sentence-transformers:80
      # Other settings
      - GRPC_SSL_CIPHER_SUITES=HIGH+ECDSA:HIGH+aRSA
      - NODE_TLS_REJECT_UNAUTHORIZED=0
      - NVIDIA_API_KEY=${NVIDIA_API_KEY:-}
      - NODE_OPTIONS=--max-http-header-size=80000
      - UV_THREADPOOL_SIZE=128
      - HTTP_TIMEOUT=1800000
      - REQUEST_TIMEOUT=1800000
    networks:
      - default
      - txt2kg-network
      - qdrant-net
    depends_on:
      arangodb:
        condition: service_started
      ollama:
        condition: service_started

  # ArangoDB - Graph database
  arangodb:
    image: arangodb:latest
    ports:
      - '8529:8529'
    environment:
      - ARANGO_NO_AUTH=1
    volumes:
      - arangodb_data:/var/lib/arangodb3
      - arangodb_apps_data:/var/lib/arangodb3-apps
    networks:
      - default
    restart: unless-stopped

  # ArangoDB initialization - create database
  arangodb-init:
    image: arangodb:latest
    depends_on:
      arangodb:
        condition: service_started
    restart: on-failure
    entrypoint: >
      sh -c "
        echo 'Waiting for ArangoDB to start...' &&
        sleep 10 &&
        echo 'Creating txt2kg database...' &&
        arangosh --server.endpoint tcp://arangodb:8529 --server.authentication false --javascript.execute-string 'try { db._createDatabase(\"txt2kg\"); console.log(\"Database txt2kg created successfully!\"); } catch(e) { if(e.message.includes(\"duplicate\")) { console.log(\"Database txt2kg already exists\"); } else { throw e; } }'
      "
    networks:
      - default

  # Ollama - Local LLM inference
  ollama:
    build:
      context: ../services/ollama
      dockerfile: Dockerfile
    image: ollama-custom:latest
    container_name: ollama-compose
    ports:
      - '11434:11434'
    volumes:
      - ollama_data:/root/.ollama
    environment:
      - NVIDIA_VISIBLE_DEVICES=all
      - NVIDIA_DRIVER_CAPABILITIES=compute,utility
      - CUDA_VISIBLE_DEVICES=0
      - OLLAMA_FLASH_ATTENTION=1
      - OLLAMA_KEEP_ALIVE=30m
      - OLLAMA_NUM_PARALLEL=4
      - OLLAMA_MAX_LOADED_MODELS=1
      - OLLAMA_KV_CACHE_TYPE=q8_0
      - OLLAMA_GPU_LAYERS=-1
      - OLLAMA_LLM_LIBRARY=cuda_v13
    networks:
      - default
    restart: unless-stopped
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [gpu]
    healthcheck:
      test: ["CMD", "ollama", "list"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # Optional: Vector search services
  sentence-transformers:
    build:
      context: ../services/sentence-transformers
      dockerfile: Dockerfile
    ports:
      - '8000:80'
    environment:
      - MODEL_NAME=all-MiniLM-L6-v2
    networks:
      - default
    restart: unless-stopped
    profiles:
      - vector-search

  qdrant:
    image: qdrant/qdrant:latest
    container_name: qdrant
    ports:
      - "6333:6333"
      - "6334:6334"
    volumes:
      - qdrant_data:/qdrant/storage
    networks:
      - qdrant-net
    restart: unless-stopped
    profiles:
      - vector-search

  qdrant-init:
    image: curlimages/curl:latest
    depends_on:
      - qdrant
    restart: "no"
    entrypoint: /bin/sh
    command:
      - -c
      - |
        echo 'Waiting for Qdrant to start...'
        sleep 5
        curl -X PUT http://qdrant:6333/collections/entity-embeddings \
          -H 'Content-Type: application/json' \
          -d '{"vectors":{"size":384,"distance":"Cosine"}}' || true
        curl -X PUT http://qdrant:6333/collections/document-embeddings \
          -H 'Content-Type: application/json' \
          -d '{"vectors":{"size":384,"distance":"Cosine"}}' || true
        echo 'Collections created'
    networks:
      - qdrant-net
    profiles:
      - vector-search

volumes:
  arangodb_data:
  arangodb_apps_data:
  ollama_data:
  qdrant_data:

networks:
  default:
    driver: bridge
  txt2kg-network:
    driver: bridge
  qdrant-net:
    name: qdrant-network
