# EDR Platform — application containers only (no Postgres).
# Requires a local .npmrc with GitHub Packages auth for @tria-plc (freight API + freight web).
# Copy apps/*/env.example to .env and set real values before `docker compose up`.
#
# Build:  DOCKER_BUILDKIT=1 docker compose build
# Run:    docker compose up -d

services:
  # Message broker for payment event delivery (payment-api -> passenger/freight).
  # Management UI: http://localhost:15672 (login edr / edr_secret). vhost: payment.
  # In deployed envs this is a shared/managed RabbitMQ; only PAYMENT_RABBITMQ_URL changes.
  rabbitmq:
    image: rabbitmq:3.13-management
    ports:
      - "5672:5672" # AMQP — apps connect here
      - "15672:15672" # management UI
    environment:
      RABBITMQ_DEFAULT_USER: edr
      RABBITMQ_DEFAULT_PASS: edr_secret
      RABBITMQ_DEFAULT_VHOST: payment
    healthcheck:
      test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  freight-api:
    build:
      context: .
      dockerfile: apps/edr-freight-api/Dockerfile
      secrets:
        - npmrc
    ports:
      - "${FREIGHT_API_PORT:-3001}:${FREIGHT_API_PORT:-3001}"
    env_file:
      - apps/edr-freight-api/.env

  passenger-api:
    build:
      context: .
      dockerfile: apps/edr-passenger-api/Dockerfile
    ports:
      - "${PASSENGER_API_PORT:-4000}:${PASSENGER_API_PORT:-4000}"
    env_file:
      - apps/edr-passenger-api/.env
    depends_on:
      rabbitmq:
        condition: service_healthy

  freight-portal:
    build:
      context: .
      dockerfile: infrastructure/docker/Dockerfile.web
      args:
        TURBO_FILTER: "@edr/freight-portal"
        APP_PATH: apps/edr-freight-web/portal
        # Browser-reachable URL; override for production deployments
        VITE_API_URL: ${FREIGHT_VITE_API_URL:-http://localhost:3001/api}
      secrets:
        - npmrc
    ports:
      - "${FREIGHT_PORTAL_PORT:-5173}:80"

  freight-backoffice:
    build:
      context: .
      dockerfile: infrastructure/docker/Dockerfile.web
      args:
        TURBO_FILTER: "@edr/freight-backoffice"
        APP_PATH: apps/edr-freight-web/backoffice
        VITE_API_URL: ${FREIGHT_VITE_API_URL:-http://localhost:3001/api}
      secrets:
        - npmrc
    ports:
      - "${FREIGHT_BACKOFFICE_PORT:-5183}:80"

  passenger-portal:
    build:
      context: .
      dockerfile: infrastructure/docker/Dockerfile.passenger-web
      args:
        APP_PACKAGE: "@edr/passenger-portal"
        APP_PATH: apps/edr-passenger-web/portal
        NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-http://localhost:4000}
      secrets:
        - npmrc
    ports:
      - "${PASSENGER_PORTAL_PORT:-5174}:${PASSENGER_PORTAL_PORT:-5174}"
    env_file:
      - apps/edr-passenger-web/portal/.env

  passenger-backoffice:
    build:
      context: .
      dockerfile: infrastructure/docker/Dockerfile.passenger-web
      args:
        APP_PACKAGE: "@edr/passenger-backoffice"
        APP_PATH: apps/edr-passenger-web/backoffice
        NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-http://localhost:4000}
      secrets:
        - npmrc
    ports:
      - "${PASSENGER_BACKOFFICE_PORT:-5184}:${PASSENGER_BACKOFFICE_PORT:-5184}"
    env_file:
      - apps/edr-passenger-web/backoffice/.env

  payment-api:
    build:
      context: .
      dockerfile: apps/edr-payment-api/Dockerfile
      args:
        APP_PACKAGE: "@edr/payment-api"
        APP_PATH: apps/edr-payment-api
      secrets:
        - npmrc
    ports:
      - "${PAYMENT_API_PORT:-3008}:${PAYMENT_API_PORT:-3008}"
    env_file:
      - apps/edr-payment-api/.env
    depends_on:
      rabbitmq:
        condition: service_healthy

secrets:
  npmrc:
    file: .npmrc
