name: nodedc-platform

services:
  reverse-proxy:
    image: ${PLATFORM_PROXY_IMAGE:-nodedc/plane-proxy:ru}
    restart: unless-stopped
    ports:
      - "${PLATFORM_HTTP_PORT:-80}:80"
    volumes:
      - ./reverse-proxy/Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy-data:/data
      - caddy-config:/config
    depends_on:
      authentik-server:
        condition: service_started
      notification-core:
        condition: service_started
      ai-workspace-hub:
        condition: service_started
      ai-workspace-assistant:
        condition: service_started
      map-gateway:
        condition: service_healthy
    extra_hosts:
      - "host.docker.internal:host-gateway"

  postgresql-authentik:
    image: docker.io/library/postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${PG_DB:-authentik}
      POSTGRES_PASSWORD: ${PG_PASS:?database password required}
      POSTGRES_USER: ${PG_USER:-authentik}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
      interval: 30s
      timeout: 5s
      retries: 5
      start_period: 20s
    volumes:
      - authentik-database:/var/lib/postgresql/data

  authentik-server:
    image: ${AUTHENTIK_IMAGE:-ghcr.io/goauthentik/server}:${AUTHENTIK_TAG:-2026.2.2}
    command: server
    restart: unless-stopped
    environment:
      AUTHENTIK_POSTGRESQL__HOST: postgresql-authentik
      AUTHENTIK_POSTGRESQL__NAME: ${PG_DB:-authentik}
      AUTHENTIK_POSTGRESQL__PASSWORD: ${PG_PASS:?database password required}
      AUTHENTIK_POSTGRESQL__USER: ${PG_USER:-authentik}
      AUTHENTIK_SECRET_KEY: ${AUTHENTIK_SECRET_KEY:?secret key required}
      AUTHENTIK_LISTEN__TRUSTED_PROXY_CIDRS: ${AUTHENTIK_LISTEN__TRUSTED_PROXY_CIDRS:-127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,::1/128}
      AUTHENTIK_ERROR_REPORTING__ENABLED: ${AUTHENTIK_ERROR_REPORTING__ENABLED:-false}
      AUTHENTIK_BOOTSTRAP_EMAIL: ${AUTHENTIK_BOOTSTRAP_EMAIL:-}
      AUTHENTIK_BOOTSTRAP_PASSWORD: ${AUTHENTIK_BOOTSTRAP_PASSWORD:-}
      AUTHENTIK_BOOTSTRAP_TOKEN: ${AUTHENTIK_BOOTSTRAP_TOKEN:-}
    depends_on:
      postgresql-authentik:
        condition: service_healthy
    expose:
      - "9000"
      - "9443"
    shm_size: 512mb
    volumes:
      - authentik-data:/data
      - ./authentik/custom-templates:/templates

  authentik-worker:
    image: ${AUTHENTIK_IMAGE:-ghcr.io/goauthentik/server}:${AUTHENTIK_TAG:-2026.2.2}
    command: worker
    restart: unless-stopped
    user: root
    environment:
      AUTHENTIK_POSTGRESQL__HOST: postgresql-authentik
      AUTHENTIK_POSTGRESQL__NAME: ${PG_DB:-authentik}
      AUTHENTIK_POSTGRESQL__PASSWORD: ${PG_PASS:?database password required}
      AUTHENTIK_POSTGRESQL__USER: ${PG_USER:-authentik}
      AUTHENTIK_SECRET_KEY: ${AUTHENTIK_SECRET_KEY:?secret key required}
      AUTHENTIK_ERROR_REPORTING__ENABLED: ${AUTHENTIK_ERROR_REPORTING__ENABLED:-false}
    depends_on:
      postgresql-authentik:
        condition: service_healthy
    shm_size: 512mb
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - authentik-data:/data
      - authentik-certs:/certs
      - ./authentik/custom-templates:/templates

  notification-postgres:
    image: docker.io/library/postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${NOTIFICATION_PG_DB:-nodedc_notifications}
      POSTGRES_PASSWORD: ${NOTIFICATION_PG_PASS:-nodedc_notifications}
      POSTGRES_USER: ${NOTIFICATION_PG_USER:-nodedc_notifications}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
      interval: 30s
      timeout: 5s
      retries: 5
      start_period: 20s
    volumes:
      - notification-database:/var/lib/postgresql/data

  notification-core:
    image: nodedc/notification-core:local
    build:
      context: ../services/notification-core
    restart: unless-stopped
    environment:
      NODE_ENV: production
      PORT: 5185
      DATABASE_URL: postgres://${NOTIFICATION_PG_USER:-nodedc_notifications}:${NOTIFICATION_PG_PASS:-nodedc_notifications}@notification-postgres:5432/${NOTIFICATION_PG_DB:-nodedc_notifications}
    expose:
      - "5185"
    depends_on:
      notification-postgres:
        condition: service_healthy

  ai-workspace-postgres:
    image: docker.io/library/postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${AI_WORKSPACE_PG_DB:-nodedc_ai_workspace}
      POSTGRES_PASSWORD: ${AI_WORKSPACE_PG_PASS:-nodedc_ai_workspace}
      POSTGRES_USER: ${AI_WORKSPACE_PG_USER:-nodedc_ai_workspace}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
      interval: 30s
      timeout: 5s
      retries: 5
      start_period: 20s
    volumes:
      - ai-workspace-database:/var/lib/postgresql/data

  ai-workspace-assistant:
    image: nodedc/ai-workspace-assistant:local
    build:
      context: ../services
      dockerfile: ai-workspace-assistant/Dockerfile.dev
    restart: unless-stopped
    environment:
      NODE_ENV: production
      PORT: 18082
      DATABASE_URL: postgres://${AI_WORKSPACE_PG_USER:-nodedc_ai_workspace}:${AI_WORKSPACE_PG_PASS:-nodedc_ai_workspace}@ai-workspace-postgres:5432/${AI_WORKSPACE_PG_DB:-nodedc_ai_workspace}
      AI_WORKSPACE_ASSISTANT_TOKEN: ${AI_WORKSPACE_ASSISTANT_TOKEN:-dev-ai-workspace-assistant-token}
      AI_WORKSPACE_HUB_PUBLIC_URL: ${AI_WORKSPACE_HUB_PUBLIC_URL:-wss://ai-hub.nodedc.ru/api/ai-workspace/hub}
      AI_WORKSPACE_HUB_INTERNAL_URL: ${AI_WORKSPACE_HUB_INTERNAL_URL:-https://ai-hub.nodedc.ru}
      AI_WORKSPACE_HUB_TOKEN: ${AI_WORKSPACE_HUB_TOKEN:-}
      AI_WORKSPACE_HUB_FALLBACK_URLS: ${AI_WORKSPACE_HUB_FALLBACK_URLS:-}
      AI_WORKSPACE_ASSISTANT_ACTION_RELAY_ENABLED: ${AI_WORKSPACE_ASSISTANT_ACTION_RELAY_ENABLED:-true}
      AI_WORKSPACE_ASSISTANT_ACTION_RELAY_ID: ${AI_WORKSPACE_ASSISTANT_ACTION_RELAY_ID:-local-dev}
      AI_WORKSPACE_ONTOLOGY_MCP_ENABLED: ${AI_WORKSPACE_ONTOLOGY_MCP_ENABLED:-true}
      AI_WORKSPACE_OPS_ENTITLEMENT_URL: ${AI_WORKSPACE_OPS_ENTITLEMENT_URL:-}
      AI_WORKSPACE_OPS_ENTITLEMENT_TOKEN: ${AI_WORKSPACE_OPS_ENTITLEMENT_TOKEN:-}
      AI_WORKSPACE_OPS_ENTITLEMENT_REQUIRED: ${AI_WORKSPACE_OPS_ENTITLEMENT_REQUIRED:-false}
      AI_WORKSPACE_ENTITLEMENT_ADAPTERS_JSON: ${AI_WORKSPACE_ENTITLEMENT_ADAPTERS_JSON:-}
      AI_WORKSPACE_OPS_GATEWAY_BASE_URL: ${AI_WORKSPACE_OPS_GATEWAY_BASE_URL:-}
      AI_WORKSPACE_OPS_DEFAULT_WORKSPACE_SLUG: ${AI_WORKSPACE_OPS_DEFAULT_WORKSPACE_SLUG:-}
      AI_WORKSPACE_OPS_DEFAULT_PROJECT_ID: ${AI_WORKSPACE_OPS_DEFAULT_PROJECT_ID:-}
      NODEDC_INTERNAL_ACCESS_TOKEN: ${NODEDC_INTERNAL_ACCESS_TOKEN:-}
      NDC_LAUNCHER_INTERNAL_URL: ${NDC_LAUNCHER_INTERNAL_URL:-http://host.docker.internal:5173}
    expose:
      - "18082"
    ports:
      - "${AI_WORKSPACE_ASSISTANT_HOST_BIND:-127.0.0.1:18082}:18082"
    depends_on:
      ai-workspace-postgres:
        condition: service_healthy

  ontology-core:
    image: nodedc/ontology-core:local
    build:
      context: ../services/ontology-core
    restart: unless-stopped
    environment:
      NODE_ENV: production
      PORT: 18104
      NODEDC_INTERNAL_ACCESS_TOKEN: ${NODEDC_INTERNAL_ACCESS_TOKEN:-}
    expose:
      - "18104"
    healthcheck:
      test:
        - CMD
        - node
        - -e
        - "fetch('http://127.0.0.1:18104/healthz').then((response) => process.exit(response.ok ? 0 : 1)).catch(() => process.exit(1))"
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s

  # Frozen legacy Gelios compatibility contour. Keep it reproducible and
  # isolated; new provider integrations use the provider-neutral data plane.
  gelios-postgres:
    image: ${GELIOS_TIMESCALE_IMAGE:-timescale/timescaledb-ha:pg16.14-ts2.28.2-all}
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${GELIOS_PG_DB:-nodedc_gelios}
      POSTGRES_USER: ${GELIOS_PG_USER:-nodedc_gelios}
      POSTGRES_PASSWORD: ${GELIOS_PG_PASS:-change-me-generate-with-infra-scripts-init-dev-env}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
      interval: 30s
      timeout: 5s
      retries: 5
      start_period: 30s
    volumes:
      - gelios-database:/home/postgres/pgdata/data
    networks:
      - gelios-data

  gelios-gateway:
    image: nodedc/gelios-gateway:local
    build:
      context: ../services/gelios-gateway
    restart: unless-stopped
    environment:
      NODE_ENV: production
      PORT: 18105
      DATABASE_URL: ${GELIOS_DATABASE_URL:-postgresql://nodedc_gelios:change-me-generate-with-infra-scripts-init-dev-env@gelios-postgres:5432/nodedc_gelios}
      NODEDC_INTERNAL_ACCESS_TOKEN: ${NODEDC_INTERNAL_ACCESS_TOKEN:-}
      GELIOS_TENANT_ID: ${GELIOS_TENANT_ID:-robot2b}
      GELIOS_CONNECTION_ID: ${GELIOS_CONNECTION_ID:-gelios-primary}
      GELIOS_UNIT_SCOPE: ${GELIOS_UNIT_SCOPE:-allowlist}
      GELIOS_ALLOWED_UNIT_IDS: ${GELIOS_ALLOWED_UNIT_IDS:-}
      GELIOS_INTAKE_ENABLED: ${GELIOS_INTAKE_ENABLED:-false}
      GELIOS_RAW_RETENTION_DAYS: ${GELIOS_RAW_RETENTION_DAYS:-14}
    expose:
      - "18105"
    ports:
      - "${GELIOS_GATEWAY_HOST_BIND:-127.0.0.1:18105}:18105"
    depends_on:
      gelios-postgres:
        condition: service_healthy
    healthcheck:
      test:
        - CMD
        - node
        - -e
        - "fetch('http://127.0.0.1:18105/healthz').then((response) => process.exit(response.ok ? 0 : 1)).catch(() => process.exit(1))"
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 15s
    networks:
      - default
      - gelios-data

  ai-workspace-hub:
    image: nodedc/ai-workspace-hub:local
    build:
      context: ../services/ai-workspace-hub
    restart: unless-stopped
    environment:
      NODE_ENV: production
      PORT: 18081
      AI_WORKSPACE_HUB_TOKEN: ${AI_WORKSPACE_HUB_TOKEN:-dev-ai-workspace-hub-token}
      AI_WORKSPACE_HUB_WS_PATH: /api/ai-workspace/hub
      NODEDC_INTERNAL_ACCESS_TOKEN: ${NODEDC_INTERNAL_ACCESS_TOKEN:-}
      NODEDC_AI_WORKSPACE_ASSISTANT_URL: http://ai-workspace-assistant:18082
      NODEDC_ONTOLOGY_CORE_URL: http://ontology-core:18104
    expose:
      - "18081"
    ports:
      - "${AI_WORKSPACE_HUB_HOST_BIND:-127.0.0.1:18081}:18081"
    depends_on:
      ontology-core:
        condition: service_healthy

  map-gateway:
    image: nodedc/map-gateway:local
    build:
      context: ../services/map-gateway
    restart: unless-stopped
    environment:
      NODE_ENV: production
      PORT: 18103
      CESIUM_ION_TOKEN: ${CESIUM_ION_TOKEN:-}
      # Foundry signs its private token-admin requests with the existing
      # Platform internal credential. The value stays outside this compose
      # file; this line only wires the local development runtime.
      NODEDC_INTERNAL_ACCESS_TOKEN: ${NODEDC_INTERNAL_ACCESS_TOKEN:-}
      CESIUM_ION_ASSET_ALLOWLIST: ${CESIUM_ION_ASSET_ALLOWLIST:-1,2,96188}
      MAP_GATEWAY_UPSTREAM_ALLOWLIST: ${MAP_GATEWAY_UPSTREAM_ALLOWLIST:-api.cesium.com,assets.ion.cesium.com,tile.openstreetmap.org,dev.virtualearth.net,ecn.t0.tiles.virtualearth.net,ecn.t1.tiles.virtualearth.net,ecn.t2.tiles.virtualearth.net,ecn.t3.tiles.virtualearth.net}
      # Offline access is selected explicitly with the cache profile, rather
      # than treating live Bing tile hosts as legacy providers globally.
      MAP_GATEWAY_LEGACY_CACHE_HOSTS: ${MAP_GATEWAY_LEGACY_CACHE_HOSTS:-}
      MAP_GATEWAY_OFFLINE_PROVIDER_ALLOWLIST: ${MAP_GATEWAY_OFFLINE_PROVIDER_ALLOWLIST:-}
      MAP_GATEWAY_CORS_ORIGIN: ${MAP_GATEWAY_CORS_ORIGIN:-http://127.0.0.1:3333,http://localhost:3333}
      MAP_GATEWAY_ALLOW_ANONYMOUS: ${MAP_GATEWAY_ALLOW_ANONYMOUS:-true}
      # The mutable live cache and the immutable imported Engine snapshot are
      # separate stores. Selecting the offline adapter can never write to or
      # fetch through the live store.
      MAP_CACHE_DIR: /var/lib/nodedc-map-live-cache
      MAP_OFFLINE_SNAPSHOT_DIR: /var/lib/nodedc-map-offline-snapshot
      MAP_CACHE_MODE: ${MAP_CACHE_MODE:-readwrite}
      MAP_CACHE_MAX_MB: ${MAP_CACHE_MAX_MB:-20480}
      MAP_CACHE_MAX_OBJECT_MB: ${MAP_CACHE_MAX_OBJECT_MB:-128}
      MAP_CACHE_DEFAULT_TTL_SECONDS: ${MAP_CACHE_DEFAULT_TTL_SECONDS:-604800}
      MAP_GATEWAY_UPSTREAM_TIMEOUT_SECONDS: ${MAP_GATEWAY_UPSTREAM_TIMEOUT_SECONDS:-30}
    expose:
      - "18103"
    ports:
      - "${MAP_GATEWAY_HOST_BIND:-127.0.0.1:18103}:18103"
    volumes:
      - map-live-tile-cache:/var/lib/nodedc-map-live-cache
      - map-offline-snapshot:/var/lib/nodedc-map-offline-snapshot:ro
    healthcheck:
      test:
        - CMD
        - node
        - -e
        - "fetch('http://127.0.0.1:18103/healthz').then((response) => process.exit(response.ok ? 0 : 1)).catch(() => process.exit(1))"
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s

volumes:
  authentik-database:
  authentik-data:
  authentik-certs:
  notification-database:
  ai-workspace-database:
  gelios-database:
  caddy-data:
  caddy-config:
  # Cache data is runtime state, not Compose lifecycle state. These volumes are
  # created once by Platform bootstrap and deliberately survive `down -v`.
  # Removing them requires an explicit `docker volume rm` operation.
  map-live-tile-cache:
    external: true
    name: ${NODEDC_MAP_LIVE_CACHE_VOLUME:-nodedc-platform_map-live-tile-cache}
  map-offline-snapshot:
    external: true
    name: ${NODEDC_MAP_OFFLINE_SNAPSHOT_VOLUME:-nodedc-platform_map-offline-snapshot}

networks:
  gelios-data:
    internal: true
