services:
  owl_imports_combiner:
    build: ./python_services/owl_imports_combiner
    environment:
      - COMBINE_OWL_IMPORTS_ON_START=${COMBINE_OWL_IMPORTS_ON_START:-false}
      - COMBINE_ENTRY_LOCATION
      - COMBINE_OUTPUT_LOCATION
      - COMBINE_OUTPUT_NAME
      - COMBINE_FORCE=${COMBINE_FORCE:-false}
      - TTL_PATH=${TTL_PATH:-/data/o3po.ttl}
    volumes:
      - ./data:/data:Z

  radial_sugiyama:
    profiles: ["radial"]
    build: ./radial_sugiyama
    working_dir: /workspace
    env_file:
      - ./radial_sugiyama/.env
    volumes:
      - ./radial_sugiyama:/workspace:Z
    restart: "no"

  backend:
    build:
      context: .
      dockerfile: backend_go/Dockerfile
    env_file:
      - ./radial_sugiyama/.env
    ports:
      - "8000:8000"
    environment:
      - DEFAULT_NODE_LIMIT=${DEFAULT_NODE_LIMIT:-800000}
      - DEFAULT_EDGE_LIMIT=${DEFAULT_EDGE_LIMIT:-2000000}
      - MAX_NODE_LIMIT=${MAX_NODE_LIMIT:-10000000}
      - MAX_EDGE_LIMIT=${MAX_EDGE_LIMIT:-20000000}
      - INCLUDE_BNODES=${INCLUDE_BNODES:-false}
      - CORS_ORIGINS=${CORS_ORIGINS:-http://localhost:5173}
      - SPARQL_SOURCE_MODE=${SPARQL_SOURCE_MODE:-local}
      - SPARQL_HOST=${SPARQL_HOST:-http://anzograph:8080}
      - SPARQL_ENDPOINT
      - EXTERNAL_SPARQL_ENDPOINT
      - KEYCLOAK_TOKEN_ENDPOINT
      - KEYCLOAK_CLIENT_ID
      - KEYCLOAK_USERNAME
      - KEYCLOAK_PASSWORD
      - KEYCLOAK_SCOPE=${KEYCLOAK_SCOPE:-openid}
      - ACCESS_TOKEN
      - SPARQL_USER=${SPARQL_USER:-admin}
      - SPARQL_PASS=${SPARQL_PASS:-Passw0rd1}
      - SPARQL_DATA_FILE=${SPARQL_DATA_FILE:-file:///opt/shared-files/o3po.ttl}
      - SPARQL_GRAPH_IRI
      - SPARQL_LOAD_ON_START=${SPARQL_LOAD_ON_START:-false}
      - SPARQL_TIMEOUT_S=${SPARQL_TIMEOUT_S:-300}
      - SPARQL_READY_RETRIES=${SPARQL_READY_RETRIES:-30}
      - SPARQL_READY_DELAY_S=${SPARQL_READY_DELAY_S:-4}
      - SPARQL_READY_TIMEOUT_S=${SPARQL_READY_TIMEOUT_S:-10}
      - EDGE_BATCH_SIZE=${EDGE_BATCH_SIZE:-100000}
      - FREE_OS_MEMORY_AFTER_SNAPSHOT=${FREE_OS_MEMORY_AFTER_SNAPSHOT:-false}
      - LOG_SNAPSHOT_TIMINGS=${LOG_SNAPSHOT_TIMINGS:-false}
      - HIERARCHY_LAYOUT_ENGINE=${HIERARCHY_LAYOUT_ENGINE:-go}
      - HIERARCHY_LAYOUT_BRIDGE_BIN=${HIERARCHY_LAYOUT_BRIDGE_BIN:-/app/radial_sugiyama_go_bridge}
      - HIERARCHY_LAYOUT_BRIDGE_WORKDIR=${HIERARCHY_LAYOUT_BRIDGE_WORKDIR:-/workspace/radial_sugiyama}
      - HIERARCHY_LAYOUT_TIMEOUT_S=${HIERARCHY_LAYOUT_TIMEOUT_S:-60}
      - HIERARCHY_LAYOUT_ROOT_IRI=${HIERARCHY_LAYOUT_ROOT_IRI:-http://purl.obolibrary.org/obo/BFO_0000001}
    depends_on:
      owl_imports_combiner:
        condition: service_completed_successfully
      anzograph:
        condition: service_started
    volumes:
      - ./data:/data:Z
      - ./radial_sugiyama:/workspace/radial_sugiyama:Z
    healthcheck:
      test: ["CMD", "curl", "-fsS", "http://localhost:8000/api/health"]
      interval: 5s
      timeout: 3s
      retries: 60

  frontend:
    build: ./frontend
    ports:
      - "5173:5173"
    environment:
      - VITE_BACKEND_URL=${VITE_BACKEND_URL:-http://backend:8000}
      - VITE_COSMOS_ENABLE_SIMULATION=${VITE_COSMOS_ENABLE_SIMULATION:-true}
      - VITE_COSMOS_DEBUG_LAYOUT=${VITE_COSMOS_DEBUG_LAYOUT:-false}
      - VITE_COSMOS_SPACE_SIZE=${VITE_COSMOS_SPACE_SIZE:-4096}
      - VITE_COSMOS_CURVED_LINKS=${VITE_COSMOS_CURVED_LINKS:-true}
      - VITE_COSMOS_FIT_VIEW_PADDING=${VITE_COSMOS_FIT_VIEW_PADDING:-0.12}
      - VITE_COSMOS_SIMULATION_DECAY=${VITE_COSMOS_SIMULATION_DECAY:-5000}
      - VITE_COSMOS_SIMULATION_GRAVITY=${VITE_COSMOS_SIMULATION_GRAVITY:-0}
      - VITE_COSMOS_SIMULATION_CENTER=${VITE_COSMOS_SIMULATION_CENTER:-0.05}
      - VITE_COSMOS_SIMULATION_REPULSION=${VITE_COSMOS_SIMULATION_REPULSION:-0.5}
      - VITE_COSMOS_SIMULATION_LINK_SPRING=${VITE_COSMOS_SIMULATION_LINK_SPRING:-1}
      - VITE_COSMOS_SIMULATION_LINK_DISTANCE=${VITE_COSMOS_SIMULATION_LINK_DISTANCE:-10}
      - VITE_COSMOS_SIMULATION_FRICTION=${VITE_COSMOS_SIMULATION_FRICTION:-0.1}
    volumes:
      - ./frontend:/app
      - /app/node_modules
    depends_on:
      backend:
        condition: service_healthy
  
  anzograph:
    image: cambridgesemantics/anzograph:latest
    container_name: anzograph
    mem_limit: 20g
    ports:
      - "8080:8080"
      - "8443:8443"
    volumes:
      - ./data:/opt/shared-files:Z
      - ./data/app_home:/opt/anzograph/app-home:Z
      - ./data/persistence:/opt/anzograph/persistence:Z
      - ./data/config:/opt/anzograph/config:Z
      - ./data/internal:/opt/anzograph/internal:Z
