version: "3.8"

services:
  # Vector Database - Qdrant
  qdrant-node2:
    image: qdrant/qdrant:v1.12.4
    container_name: dagi-qdrant-node2
    ports:
      - "6333:6333"
      - "6334:6334"
    volumes:
      - ./data/qdrant-node2:/qdrant/storage
    environment:
      - QDRANT__SERVICE__HOST=0.0.0.0
      - QDRANT__SERVICE__GRPC_PORT=6334
    networks:
      dagi-memory-network:
        aliases:
          - qdrant
          - dagi-qdrant
          - qdrant-node2
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "bash -lc '</dev/tcp/127.0.0.1/6333'"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s

  # Relational Database - PostgreSQL
  postgres-node2:
    image: postgres:16-alpine
    container_name: dagi-postgres-node2
    ports:
      - "5433:5432"
    environment:
      - POSTGRES_DB=daarion_memory
      - POSTGRES_USER=daarion
      - POSTGRES_PASSWORD=daarion_secret_node2
      - POSTGRES_INITDB_ARGS=--encoding=UTF-8 --locale=en_US.UTF-8
    volumes:
      - ./data/postgres-node2:/var/lib/postgresql/data
      - ./services/memory-service/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    networks:
      dagi-memory-network:
        aliases:
          - postgres
          - dagi-postgres
          - postgres-node2
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U daarion -d daarion_memory"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  # Graph Database - Neo4j (Optional but recommended)
  neo4j-node2:
    image: neo4j:5.15-community
    container_name: dagi-neo4j-node2
    ports:
      - "7474:7474"
      - "7687:7687"
    environment:
      - NEO4J_AUTH=neo4j/daarion_node2_secret
      - NEO4J_server_memory_heap_initial__size=512m
      - NEO4J_server_memory_heap_max__size=1G
      - NEO4J_server_memory_pagecache_size=512m
      - NEO4J_dbms_security_procedures_unrestricted=apoc.*
      - NEO4J_dbms_security_allow__csv__import__from__file__urls=true
    volumes:
      - ./data/neo4j-node2:/data
      - ./data/neo4j-node2/logs:/logs
    networks:
      dagi-memory-network:
        aliases:
          - neo4j
          - dagi-neo4j
          - neo4j-node2
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "wget -q --spider http://localhost:7474 || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Memory Service
  memory-service-node2:
    build:
      context: ./services/memory-service
      dockerfile: Dockerfile
    container_name: dagi-memory-service-node2
    ports:
      - "8000:8000"
    environment:
      # Settings in app/config.py use env_prefix="MEMORY_"
      - MEMORY_SERVICE_NAME=memory-service
      - MEMORY_POSTGRES_HOST=postgres-node2
      - MEMORY_POSTGRES_PORT=5432
      - MEMORY_POSTGRES_USER=daarion
      - MEMORY_POSTGRES_PASSWORD=daarion_secret_node2
      - MEMORY_POSTGRES_DB=daarion_memory
      - MEMORY_QDRANT_HOST=qdrant-node2
      - MEMORY_QDRANT_PORT=6333
      - MEMORY_COHERE_API_KEY=${COHERE_API_KEY}
      - MEMORY_DEEPSEEK_API_KEY=${DEEPSEEK_API_KEY}

      # Qdrant configuration
      - QDRANT_HOST=qdrant-node2
      - QDRANT_PORT=6333
      - QDRANT_GRPC_PORT=6334
      
      # PostgreSQL configuration
      - DATABASE_URL=postgresql://daarion:daarion_secret_node2@postgres-node2:5432/daarion_memory
      - DB_HOST=postgres-node2
      - DB_PORT=5432
      - DB_NAME=daarion_memory
      - DB_USER=daarion
      - DB_PASSWORD=daarion_secret_node2
      
      # Neo4j configuration (optional)
      - NEO4J_URI=bolt://neo4j-node2:7687
      - NEO4J_USER=neo4j
      - NEO4J_PASSWORD=daarion_node2_secret
      
      # Cohere API for embeddings
      - COHERE_API_KEY=${COHERE_API_KEY}
      
      # Node identification
      - NODE_ID=node2
      - NODE_ENV=development
      
      # Logging
      - LOG_LEVEL=INFO
      - PYTHONUNBUFFERED=1
      
      # Memory settings
      - MEMORY_MODE=local
      - EMBEDDING_MODEL=embed-multilingual-v3.0
      - EMBEDDING_DIMENSION=1024
      
      # Optional: Remote NODA1 access (hybrid mode)
      # Uncomment to enable read access to NODA1
      # - REMOTE_QDRANT_HOST=144.76.224.179
      # - REMOTE_QDRANT_PORT=6333
      # - REMOTE_DATABASE_URL=postgresql://daarion_reader:***@144.76.224.179:5432/daarion_memory
      # - READ_ONLY_MODE=false
    depends_on:
      qdrant-node2:
        condition: service_healthy
      postgres-node2:
        condition: service_healthy
      neo4j-node2:
        condition: service_healthy
    networks:
      dagi-memory-network:
        aliases:
          - memory-service
          - memory-service-node2
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    volumes:
      - /Users/apple/Desktop/R&D:/vault/rd:ro
      - /Users/apple/Documents/Obsidian Vault:/vault/obsidian:ro

  # Redis for caching (optional but recommended)
  redis-node2:
    image: redis:7-alpine
    container_name: dagi-redis-node2
    ports:
      - "6379:6379"
    volumes:
      - ./data/redis-node2:/data
    networks:
      - dagi-memory-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Adminer - Database management UI (optional)
  adminer:
    image: adminer:latest
    container_name: dagi-adminer-node2
    ports:
      - "8080:8080"
    networks:
      - dagi-memory-network
    restart: unless-stopped
    depends_on:
      - postgres-node2

networks:
  dagi-memory-network:
    driver: bridge
    name: dagi-memory-network-node2

volumes:
  qdrant-data:
  postgres-data:
  neo4j-data:
  redis-data:
