services:
  backend:
    build: ./backend
    ports:
      - "8000:8000"
    environment:
      - GRAPH_BACKEND=${GRAPH_BACKEND:-rdflib}
      - TTL_PATH=${TTL_PATH:-/data/o3po.ttl}
      - INCLUDE_BNODES=${INCLUDE_BNODES:-false}
      - MAX_TRIPLES
      - CORS_ORIGINS=${CORS_ORIGINS:-http://localhost:5173}
      - SPARQL_HOST=${SPARQL_HOST:-http://anzograph:8080}
      - SPARQL_ENDPOINT
      - 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_CLEAR_ON_START=${SPARQL_CLEAR_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}
      - 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}
    volumes:
      - ./backend:/app
      - ./data:/data:Z
    command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
    healthcheck:
      test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/api/health').read()"]
      interval: 5s
      timeout: 3s
      retries: 60

  frontend:
    build: ./frontend
    ports:
      - "5173:5173"
    environment:
      - VITE_BACKEND_URL=${VITE_BACKEND_URL:-http://backend:8000}
    volumes:
      - ./frontend:/app
      - /app/node_modules
    depends_on:
      - backend
    # Docker Compose v1 doesn't support depends_on:condition. Do an explicit wait here.
    command: sh -c "until wget -qO- http://backend:8000/api/health >/dev/null 2>&1; do echo 'waiting for backend...'; sleep 1; done; npm run dev -- --host --port 5173"
  
  anzograph:
    image: cambridgesemantics/anzograph:latest
    container_name: anzograph
    ports:
      - "8080:8080"
      - "8443:8443"
    volumes:
      - ./data:/opt/shared-files:Z
