services:
  trex:
    command: npm run startdev
    build:
      context: ./services/trex
      dockerfile: ./Dockerfile
    ports:
      - 9229:9229
      - 25432:5432
      - 25433:5433
      - 33000:33000
    volumes:
      - ${D2E_FUNCTIONS:-./functions}:/usr/src/plugins/d2ef
      - ${D2E_FHIRFUNCTIONS:-./fhir_functions}:/usr/src/plugins/fhir
      - ./plugins/chat_functions:/usr/src/plugins/chat
      - ./ui/package.json:/usr/src/plugins/d2e-ui/package.json # For local ui development only
      - ./ui/resources:/usr/src/local-resources # For local ui development only
      # - ./services/trex/core:/usr/src/core # For debugging trex with local code
      # - ./services/trex/deno.json:/usr/src/deno.json 
      # - ./flows/base/package.json:/usr/src/plugins/d2e-flows/package.json # For local flows development only
    environment:
      RUST_LOG: ${RUST_LOG:-info}
      RUST_BACKTRACE: ${RUST_BACKTRACE:-full}
      TREX_DEBUG_GC: ${TREX_DEBUG_GC:-0}
      LOCAL_DEBUG: "true"
      DATABASE_CREDENTIALS: ${DATABASE_CREDENTIALS:-{}}
      PLUGINS_SEED: ${PLUGINS_SEED:-["d2e-notebook-plugin", "d2e-flows", "d2e-ui", "d2e-atlas", "data-transformation-flow", "hades-flow", "trex-hana", "trex-pgwire", "d2e-fhir-server", "storage"]}
      PLUGINS_SEED_UPDATE: ${PLUGINS_SEED_UPDATE:-true}
      PLUGINS_DEV_PATH: "./plugins"
      SQL_QUERY_TEMPLATES: '{"cohort-members": "SELECT {{COHORT_ID}} AS cohort_id, CURRENT_TIMESTAMP AS exported_at", "patient-count": "SELECT COUNT(*) AS patient_count FROM {{SCHEMA}}.person"}'
      WATCH: |-
        {
          "analytics-svc": false,
          "mri-pg-config": false,
          "alp-fhir-gateway": false,
          "alp-dataflow-gen-init": false,
          "query-gen-svc": false,
          "alp-usermgmt": false,
          "bookmark-svc": false,
          "mri-pa-config": false,
          "cdw-svc": false,
          "jobplugins": false,
          "dataset": false,
          "alpdb": false,
          "concept-mapping": false,
          "terminology-svc": false,
          "code-suggestion": false,
          "data-mapping": false,
          "portal": false,
          "files-manager": false,
          "demo": false,
          "d2e-webapi": false,
          "perseus": false,
          "white-rabbit": false,
          "strategus-analysis": false,
          "mcp-server": false
        }

  alp-caddy:
    ports:
      - 41000:41000
      - 41130:41130
    environment:
      CADDY__FHIR_SERVER__PUBLIC_FQDN: ${CADDY__FHIR_SERVER__PUBLIC_FQDN:-localhost}
      CADDY__CORS_LOCAL_UI: "import cors"

  alp-dataflow-gen:
    ports:
      - 41120:41120
    environment:
      SYSTEM_PORTAL__API_URL: https://trex:33000/system-portal/
  alp-dataflow-gen-worker:
    build:
      context: ./services/alp-dataflow-gen-worker
      dockerfile: Dockerfile

  alp-logto:
    build:
      context: ./services/alp-logto
      dockerfile: Dockerfile
    # volumes:
    #   - ./services/alp-logto/connector-alp-azuread/lib:/etc/logto/packages/connectors/connector-alp-azuread/lib

  alp-minerva-pg-mgmt-init:
    ports:
      - 9211:9211
    build:
      context: services/alp-pg-management
      dockerfile: Dockerfile
    volumes:
      - ./services/alp-pg-management/app:/home/docker/app

  alp-minerva-postgres:
    ports:
      - 41190:5432
      - 5432:5432

  demodb:
    ports:
      - "15432:5432"

  dicom-server:
    # uses 8042 port on the container
    ports:
      - 8042:8042

  alp-logto-post-init:
    build:
      context: ./services/alp-logto/post-init
      dockerfile: Dockerfile
    # volumes:
    #   - ./services/alp-logto/post-init:/home/docker

  # alp-fhir-server-post-init:
  #   build:
  #     context: .
  #     dockerfile: ./services/alp-fhir/Dockerfile.client
  #   volumes:
  #     - ./services/alp-fhir/dist:/home/docker/src/alp-data-node/app/dist
  #     - ./services/alp-fhir/src:/home/docker/src/alp-data-node/app/src
  #   environment:
  #     LOCAL_DEBUG: "true"
  #   ports:
  #     - 9226:9226


  enterprise-gateway:
    build:
      context: ./services/enterprise-gateway
      dockerfile: Dockerfile
    environment:
      EG_ALLOW_ORIGIN: "https://${CADDY__D2E__PUBLIC_FQDN:-${CADDY__ALP__PUBLIC_FQDN:-localhost}}${PORT:+:${PORT:-41100}}"
    volumes:
      - ./services/enterprise-gateway/r-strategus-lib/rD2E:/app/rD2E
      - ./services/enterprise-gateway/kernels/R_ohdsi_docker:/usr/local/share/jupyter/kernels/R_ohdsi_docker
  
  enterprise-gateway-viewer:
    build:
      context: ./services/enterprise-gateway
      dockerfile: Dockerfile
    environment:
      EG_ALLOW_ORIGIN: "https://${CADDY__D2E__PUBLIC_FQDN:-${CADDY__ALP__PUBLIC_FQDN:-localhost}}${PORT:+:${PORT:-41100}}"
    volumes:
      - ./services/enterprise-gateway/r-strategus-lib/rD2E:/app/rD2E
      - ./services/enterprise-gateway/kernels/R_ohdsi_docker:/usr/local/share/jupyter/kernels/R_ohdsi_docker

  supabase-storage:
    build:
      context: ./services/supabase-storage
      dockerfile: Dockerfile

  logging:
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - DOZZLE_ENABLE_SHELL=${DOZZLE_ENABLE_SHELL:-false}
      - DOZZLE_ENABLE_ACTIONS=true

  materialize-cohorts:
    profiles:
      - materialize-cohorts
    networks:
      alp:
        priority: 20
    command: npm run start
    hostname: ${PROJECT_NAME:-d2e}-materialize-cohorts
    image: ${DOCKER_IMAGE_PREFIX:-ghcr.io/ohdsi/}d2e-materialize-cohorts:${DOCKER_TAG_NAME:-develop}
    build:
      context: .
      dockerfile: ./services/materialize-cohorts/Dockerfile
    volumes:
      - services/materialize-cohorts/PKI/server.key:/app/PKI/server.key
      - services/materialize-cohorts/PKI/server.crt:/app/PKI/server.crt
      - services/materialize-cohorts/PKI/ca.crt:/app/PKI/ca.crt
    environment:
      PORT: 3333
      API_MTLS_ENABLED: ${MATERIALIZE_COHORTS_API_MTLS_ENABLED:-true}
      API_TLS_KEY_PATH: ${MATERIALIZE_COHORTS_API_TLS_KEY_PATH:-/app/PKI/server.key}
      API_TLS_CERT_PATH: ${MATERIALIZE_COHORTS_API_TLS_CERT_PATH:-/app/PKI/server.crt}
      API_TLS_CA_PATH: ${MATERIALIZE_COHORTS_API_TLS_CA_PATH:-/app/PKI/ca.crt}
    healthcheck:
      test: ["CMD-SHELL", "curl -fsSk https://127.0.0.1:3333/health"]
      interval: 10s
      timeout: 10s
      retries: 3
      start_period: 10s
