services:
  edge:
    image: "${REGISTRY}/travel-edge:${IMAGE_TAG}"
    build:
      context: .
      dockerfile: docker/edge.Dockerfile
    ports:
      - "${APP_HTTPS_PORT:-443}:443"
    volumes:
      - "${TLS_CERT_FILE}:/run/tls/tls.crt:ro"
      - "${TLS_KEY_FILE}:/run/tls/tls.key:ro"
    depends_on:
      api:
        condition: service_healthy
    networks: [travel]
    restart: unless-stopped

  api:
    image: "${REGISTRY}/travel-api:${IMAGE_TAG}"
    build:
      context: .
      dockerfile: docker/api.Dockerfile
    expose:
      - "3000"
    environment:
      DATABASE_URL: "postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}"
      REDIS_URL: "redis://redis:6379"
      APP_VERSION: "${APP_VERSION:-dev}"
      TEAMCITY_BUILD_NUMBER: "${TEAMCITY_BUILD_NUMBER:-local}"
      SOURCE_REVISION: "${SOURCE_REVISION:-local}"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "node -e \"fetch('http://127.0.0.1:3000/health/ready').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))\""]
      interval: 5s
      timeout: 3s
      retries: 20
      start_period: 10s
    networks: [travel]
    restart: unless-stopped

  worker:
    image: "${REGISTRY}/travel-worker:${IMAGE_TAG}"
    build:
      context: .
      dockerfile: docker/worker.Dockerfile
    environment:
      DATABASE_URL: "postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}"
      REDIS_URL: "redis://redis:6379"
      APP_VERSION: "${APP_VERSION:-dev}"
      TEAMCITY_BUILD_NUMBER: "${TEAMCITY_BUILD_NUMBER:-local}"
      SOURCE_REVISION: "${SOURCE_REVISION:-local}"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks: [travel]
    restart: unless-stopped

  postgres:
    image: "postgres:${POSTGRES_IMAGE_TAG:-18.4-alpine}"
    environment:
      POSTGRES_DB: "${POSTGRES_DB}"
      POSTGRES_USER: "${POSTGRES_USER}"
      POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
    expose:
      - "5432"
    volumes:
      - postgres_data:/var/lib/postgresql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 5s
      timeout: 3s
      retries: 20
    networks: [travel]
    restart: unless-stopped

  redis:
    image: "redis:${REDIS_IMAGE_TAG:-8.8.1-alpine}"
    expose:
      - "6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 20
    networks: [travel]
    restart: unless-stopped

volumes:
  postgres_data:

networks:
  travel:
    driver: bridge
