# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

# Production Docker Compose for AWS Deployment
# Note: 'version' field is deprecated in Docker Compose v2+
# This includes all services: ResilientDB, PostgreSQL (for Nexus), and GraphQ-LLM Backend

services:
  # PostgreSQL + GraphQ-LLM Backend Container
  graphql-nexus:
    build:
      context: .
      dockerfile: Dockerfile.all-in-one
      args:
        - ARCH=${ARCH:-arm64}
    container_name: graphql-nexus
    ports:
      - "5432:5432"    # PostgreSQL
      - "3001:3001"    # GraphQ-LLM Backend
    volumes:
      - nexus-postgres-data:/var/lib/postgresql/data
      - huggingface-cache:/root/.cache/huggingface
      - ./docs:/app/graphq-llm/docs:ro
    networks:
      - graphq-llm-network
    environment:
      # PostgreSQL Configuration
      - POSTGRES_USER=${POSTGRES_USER:-nexus}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-nexus}
      - POSTGRES_DB=${POSTGRES_DB:-nexus}
      # GraphQ-LLM Configuration (connects to ResilientDB sidecar)
      - RESILIENTDB_GRAPHQL_URL=http://graphql-nexus-resilientdb:5001/graphql
      - NEXUS_API_URL=${NEXUS_API_URL:-http://localhost:3000}
      - LLM_PROVIDER=${LLM_PROVIDER:-local}
      - LLM_MODEL=${LLM_MODEL:-Xenova/gpt2}
      - EMBEDDINGS_PROVIDER=${EMBEDDINGS_PROVIDER:-local}
      - HUGGINGFACE_API_KEY=${HUGGINGFACE_API_KEY:-}
      - MCP_SERVER_PORT=3001
      - MCP_SERVER_HOST=0.0.0.0
      - LOG_LEVEL=${LOG_LEVEL:-info}
      - NODE_ENV=production
    depends_on:
      - graphql-nexus-resilientdb
    healthcheck:
      test: ["CMD", "/app/healthcheck-all.sh"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 120s
    restart: unless-stopped

  # ResilientDB Sidecar (runs alongside graphql-nexus)
  graphql-nexus-resilientdb:
    image: expolab/resdb:${ARCH:-arm64}
    container_name: graphql-nexus-resilientdb
    ports:
      - "18000:18000"  # ResilientDB KV
      - "5001:5001"    # ResilientDB GraphQL
    volumes:
      - resilientdb-data:/data
      - ./setup-graphql.sh:/app/setup-graphql.sh:ro
    networks:
      - graphq-llm-network
    environment:
      - RESDB_HOST=0.0.0.0
      - RESDB_PORT=18000
    # Use default entrypoint to start KV service, then setup GraphQL
    # Don't override entrypoint - let default start KV service
    # GraphQL setup will be done manually or via a separate init container
    # For now, just start the container normally
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:18000/v1/transactions/test || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 60s
    restart: unless-stopped

  # Nexus (Next.js Application)
  # NOTE: Nexus must be cloned first. Run: ./setup-nexus.sh
  # Or manually: git clone https://github.com/sophiequynn/nexus.git ./nexus
  nexus:
    build:
      context: ./nexus
      dockerfile: ../Dockerfile.nexus
    container_name: nexus
    ports:
      - "3000:3000"
    environment:
      # Database Configuration (connect to unified graphql-nexus service)
      - DATABASE_URL=postgres://${POSTGRES_USER:-nexus}:${POSTGRES_PASSWORD:-nexus}@graphql-nexus:5432/${POSTGRES_DB:-nexus}
      
      # Embedding Configuration
      - EMBEDDING_DIM=${EMBEDDING_DIM:-384}
      
      # API Keys (Optional)
      - DEEPSEEK_API_KEY=${DEEPSEEK_API_KEY:-}
      - GEMINI_API_KEY=${GEMINI_API_KEY:-}
      - LLAMA_CLOUD_API_KEY=${LLAMA_CLOUD_API_KEY:-}
      - TAVILY_API_KEY=${TAVILY_API_KEY:-}
      
      # Supabase Configuration (Optional)
      - SUPABASE_URL=${SUPABASE_URL:-}
      - SUPABASE_ANON_KEY=${SUPABASE_ANON_KEY:-}
      
      # GraphQ-LLM Backend URL (connect to unified graphql-nexus service)
      - GRAPHQ_LLM_API_URL=http://graphql-nexus:3001
      - NEXT_PUBLIC_GRAPHQ_LLM_API_URL=http://graphql-nexus:3001
      
      - NODE_ENV=production
    depends_on:
      graphql-nexus:
        condition: service_healthy
      graphql-nexus-resilientdb:
        condition: service_healthy
    networks:
      - graphq-llm-network
    volumes:
      # Mount documents directory if needed
      - ./nexus/documents:/app/documents:ro
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000/api/research/documents || exit 0"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s  # Next.js build takes time

networks:
  graphq-llm-network:
    driver: bridge
    name: graphq-llm-network

volumes:
  resilientdb-data:
    name: resilientdb-data
  nexus-postgres-data:
    name: nexus-postgres-data
  huggingface-cache:
    name: huggingface-cache

