services:
  postgres:
    image: postgres:18.3
    container_name: postgres
    ports:
      - "5434:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    volumes:
      - postgres_data:/var/lib/postgresql
      - ./docker/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5

  rabbitmq:
    image: rabbitmq:3-management
    container_name: rabbitmq
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      RABBITMQ_DEFAULT_USER: admin
      RABBITMQ_DEFAULT_PASS: admin
#    healthcheck:
#      test: ["CMD", "rabbitmq-diagnostics", "check_port_connectivity"]
#      interval: 5s
#      timeout: 5s
#      retries: 5

  eureka:
    build: ./eurekaServer
    container_name: eureka
    ports:
      - "8761:8761"
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:8761 || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 10

  keycloak:
    image: quay.io/keycloak/keycloak:26.5.6
    command: start-dev --import-realm
    ports:
      - "8060:8060"
    volumes:
      - ./docker/keycloak/imports:/opt/keycloak/data/import
      - ./docker/keycloak/providers:/opt/keycloak/providers
      - ./docker/keycloak/themes/boat-theme:/opt/keycloak/themes/boat-theme
    environment:
      KC_BOOTSTRAP_ADMIN_USERNAME: admin
      KC_BOOTSTRAP_ADMIN_PASSWORD: admin
      KC_HTTP_ENABLED: "true"
      KC_HOSTNAME_STRICT: "false"
      KC_HOSTNAME_STRICT_HTTPS: "false"
      KC_HTTP_RELATIVE_PATH: "/"
      KC_HOSTNAME: localhost
      KC_HTTP_PORT: 8060
      KC_EVENTS_ENABLED: "true"
      KC_EVENTS_LISTENERS: "userservice-sync-listener"
      KC_ADMIN_EVENTS_ENABLED: "true"
      KC_ADMIN_EVENTS_DETAILS_ENABLED: "true"
      WEBHOOK_KEYCLOAK_SECRET: c8d904f5-5322-4d56-affb-fe63b9c436fb
#    healthcheck:
#      test: ["CMD-SHELL", "timeout 2 bash -c '</dev/tcp/127.0.0.1/8060'"]
#      interval: 10s
#      timeout: 5s
#      retries: 15

  gateway:
    build: ./springGateway
    container_name: gateway
    ports:
      - "8080:8080"
    env_file:
      - ./springGateway/.env.docker
    environment:
      OTEL_SDK_DISABLED: "false"
      OTEL_TRACES_EXPORTER: otlp
      OTEL_EXPORTER_OTLP_PROTOCOL: grpc
      OTEL_EXPORTER_OTLP_ENDPOINT: http://jaeger:4317
      OTEL_SERVICE_NAME: gateway
      OTEL_RESOURCE_ATTRIBUTES: service.instance.id=gateway,service.namespace=boat-delivery
      OTEL_METRICS_EXPORTER: none
      OTEL_PROPAGATORS: tracecontext,baggage
      OTEL_TRACE_SAMPLER: always_on
    depends_on:
      eureka:
        condition: service_healthy
      keycloak:
        condition: service_started
      jaeger:
        condition: service_started

  user-service:
    build: ./userService
    container_name: user-service-1
    expose:
      - "8081"
    env_file:
      - ./userService/.env.docker
    environment:
      SPRING_PROFILES_ACTIVE: test-dev
      APP_SYNC_ENABLED: "true"
      APP_SYNC_ENVIRONMENT: test-dev
      APP_SYNC_MIGRATE_ON_STARTUP: "true"
      APP_SYNC_KEYCLOAK_ADMIN_URL: http://keycloak:8060
      APP_SYNC_KEYCLOAK_ADMIN_USERNAME: admin
      APP_SYNC_KEYCLOAK_ADMIN_PASSWORD: admin
      APP_SYNC_KEYCLOAK_REALM: boat-delivery-realm
      APP_SYNC_DEFAULT_PASSWORD: "password123"
      OTEL_SDK_DISABLED: "false"
      OTEL_TRACES_EXPORTER: otlp
      OTEL_EXPORTER_OTLP_PROTOCOL: grpc
      OTEL_EXPORTER_OTLP_ENDPOINT: http://jaeger:4317
      OTEL_SERVICE_NAME: user-service-1
      OTEL_RESOURCE_ATTRIBUTES: service.instance.id=user-service-1,service.namespace=boat-delivery
      OTEL_METRICS_EXPORTER: none
      OTEL_PROPAGATORS: tracecontext,baggage
      OTEL_TRACE_SAMPLER: always_on
      OTEL_INSTRUMENTATION_SERVLET_ENABLED: "true"
      OTEL_INSTRUMENTATION_HTTP_SERVER_ENABLED: "true"
      OTEL_JAVA_LOGGING_AUTO_INSTRUMENTATION_ENABLED: "true"
    depends_on:
      postgres:
        condition: service_healthy
      eureka:
        condition: service_healthy
      jaeger:
        condition: service_started

  user-service-2:
    build: ./userService
    container_name: user-service-2
    expose:
      - "8081"
    env_file:
      - ./userService/.env.docker
    environment:
      SPRING_PROFILES_ACTIVE: test-dev
      APP_SYNC_ENABLED: "false"
      APP_SYNC_ENVIRONMENT: test-dev
      APP_SYNC_MIGRATE_ON_STARTUP: "false"
      APP_SYNC_KEYCLOAK_ADMIN_URL: http://keycloak:8060
      APP_SYNC_KEYCLOAK_ADMIN_USERNAME: admin
      APP_SYNC_KEYCLOAK_ADMIN_PASSWORD: admin
      APP_SYNC_KEYCLOAK_REALM: boat-delivery-realm
      APP_SYNC_DEFAULT_PASSWORD: "password123"
      OTEL_SDK_DISABLED: "false"
      OTEL_TRACES_EXPORTER: otlp
      OTEL_EXPORTER_OTLP_PROTOCOL: grpc
      OTEL_EXPORTER_OTLP_ENDPOINT: http://jaeger:4317
      OTEL_SERVICE_NAME: user-service-2
      OTEL_RESOURCE_ATTRIBUTES: service.instance.id=user-service-2,service.namespace=boat-delivery
      OTEL_METRICS_EXPORTER: none
      OTEL_PROPAGATORS: tracecontext,baggage
      OTEL_TRACE_SAMPLER: always_on
      OTEL_INSTRUMENTATION_SERVLET_ENABLED: "true"
      OTEL_INSTRUMENTATION_HTTP_SERVER_ENABLED: "true"
      OTEL_JAVA_LOGGING_AUTO_INSTRUMENTATION_ENABLED: "true"
    depends_on:
      postgres:
        condition: service_healthy
      eureka:
        condition: service_healthy
      jaeger:
        condition: service_started

  user-service-3:
    build: ./userService
    container_name: user-service-3
    expose:
      - "8081"
    env_file:
      - ./userService/.env.docker
    environment:
      SPRING_PROFILES_ACTIVE: test-dev
      APP_SYNC_ENABLED: "false"
      APP_SYNC_ENVIRONMENT: test-dev
      APP_SYNC_MIGRATE_ON_STARTUP: "false"
      APP_SYNC_KEYCLOAK_ADMIN_URL: http://keycloak:8060
      APP_SYNC_KEYCLOAK_ADMIN_USERNAME: admin
      APP_SYNC_KEYCLOAK_ADMIN_PASSWORD: admin
      APP_SYNC_KEYCLOAK_REALM: boat-delivery-realm
      APP_SYNC_DEFAULT_PASSWORD: "password123"
      OTEL_SDK_DISABLED: "false"
      OTEL_TRACES_EXPORTER: otlp
      OTEL_EXPORTER_OTLP_PROTOCOL: grpc
      OTEL_EXPORTER_OTLP_ENDPOINT: http://jaeger:4317
      OTEL_SERVICE_NAME: user-service-3
      OTEL_RESOURCE_ATTRIBUTES: service.instance.id=user-service-2,service.namespace=boat-delivery
      OTEL_METRICS_EXPORTER: none
      OTEL_PROPAGATORS: tracecontext,baggage
      OTEL_TRACE_SAMPLER: always_on
      OTEL_INSTRUMENTATION_SERVLET_ENABLED: "true"
      OTEL_INSTRUMENTATION_HTTP_SERVER_ENABLED: "true"
      OTEL_JAVA_LOGGING_AUTO_INSTRUMENTATION_ENABLED: "true"
    depends_on:
      postgres:
        condition: service_healthy
      eureka:
        condition: service_healthy
      jaeger:
        condition: service_started

  order-service:
    build: ./order-service
    container_name: order-service
    expose:
      - "8082"
    env_file:
      - ./order-service/.env.docker
    environment:
      OTEL_SDK_DISABLED: "false"
      OTEL_TRACES_EXPORTER: otlp
      OTEL_EXPORTER_OTLP_PROTOCOL: grpc
      OTEL_EXPORTER_OTLP_ENDPOINT: http://jaeger:4317
      OTEL_SERVICE_NAME: order-service
      OTEL_RESOURCE_ATTRIBUTES: service.instance.id=order-service,service.namespace=boat-delivery
      OTEL_METRICS_EXPORTER: none
      OTEL_PROPAGATORS: tracecontext,baggage
      OTEL_TRACE_SAMPLER: always_on
    depends_on:
      postgres:
        condition: service_healthy
      eureka:
        condition: service_healthy
      keycloak:
        condition: service_started
      jaeger:
        condition: service_started

  notification-service:
    build: ./notifi-service
    container_name: notification-service
    expose:
      - "8083"
    env_file:
      - ./notifi-service/.env.docker
    environment:
      OTEL_SDK_DISABLED: "false"
      OTEL_TRACES_EXPORTER: otlp
      OTEL_EXPORTER_OTLP_PROTOCOL: grpc
      OTEL_EXPORTER_OTLP_ENDPOINT: http://jaeger:4317
      OTEL_SERVICE_NAME: notification-service
      OTEL_RESOURCE_ATTRIBUTES: service.instance.id=notification-service,service.namespace=boat-delivery
      OTEL_METRICS_EXPORTER: none
      OTEL_PROPAGATORS: tracecontext,baggage
      OTEL_TRACE_SAMPLER: always_on
      OTEL_INSTRUMENTATION_SERVLET_ENABLED: "true"
      OTEL_INSTRUMENTATION_HTTP_SERVER_ENABLED: "true"
      OTEL_JAVA_LOGGING_AUTO_INSTRUMENTATION_ENABLED: "true"
    depends_on:
      postgres:
        condition: service_healthy
#      rabbitmq:
#        condition: service_healthy
      eureka:
        condition: service_healthy
      keycloak:
        condition: service_started
      jaeger:
        condition: service_started

  front:
#   OPTION 1: DEV MODE
    image: node:25.8.1
    working_dir: /app
    volumes:
      - ./front:/app
    command: sh -c "npm install && npm run dev -- --host"
    ports:
      - "5173:5173"
    depends_on:
      gateway:
        condition: service_started
#     OPTION 2: PROD MODE
#     build: ./front
#     container_name: front-app
#     ports:
#       - "5173:80"
#     depends_on:
#       gateway:
#         condition: service_started

  payment-service:
    build: ./payment-service
    container_name: payment-service
    expose:
      - "8084"
    env_file:
      - ./payment-service/.env.docker
    environment:
      OTEL_SDK_DISABLED: "false"
      OTEL_TRACES_EXPORTER: otlp
      OTEL_EXPORTER_OTLP_PROTOCOL: grpc
      OTEL_EXPORTER_OTLP_ENDPOINT: http://jaeger:4317
      OTEL_SERVICE_NAME: payment-service
      OTEL_RESOURCE_ATTRIBUTES: service.instance.id=payment-service,service.namespace=boat-delivery
      OTEL_METRICS_EXPORTER: none
      OTEL_PROPAGATORS: tracecontext,baggage
      OTEL_TRACE_SAMPLER: always_on
      OTEL_INSTRUMENTATION_SERVLET_ENABLED: "true"
      OTEL_INSTRUMENTATION_HTTP_SERVER_ENABLED: "true"
      OTEL_JAVA_LOGGING_AUTO_INSTRUMENTATION_ENABLED: "true"
    depends_on:
      postgres:
        condition: service_healthy
#      rabbitmq:
#        condition: service_healthy
      eureka:
        condition: service_healthy
      keycloak:
        condition: service_started
      jaeger:
        condition: service_started

  stripe-cli:
    image: stripe/stripe-cli:latest
    container_name: stripe-cli-forwarder
    env_file:
      - ./payment-service/.env.docker
    command: listen --forward-to http://payment-service:8084/api/payments/webhook
    depends_on:
      - payment-service
    restart: unless-stopped

  jaeger:
    image: jaegertracing/all-in-one:1.60
    container_name: jaeger
    ports:
      - "16686:16686"
      - "4317:4317"
      - "4318:4318"
      - "14250:14250"
    volumes:
      - ./docker/jaeger/ui-config.json:/etc/jaeger/ui-config.json:ro
    environment:
      COLLECTOR_OTLP_ENABLED: "true"
      QUERY_UI_CONFIG_PATH: /etc/jaeger/ui-config.json
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:16686/"]
      interval: 10s
      timeout: 5s
      retries: 10

  keycloak-init:
    image: curlimages/curl:latest
    container_name: keycloak-init
    entrypoint: /bin/sh
    command:
      - /init-users.sh
    volumes:
      - ./docker/keycloak/imports/init-users-verbose.sh:/init-users.sh:ro
    depends_on:
      keycloak:
        condition: service_started
      user-service:
        condition: service_started

volumes:
  postgres_data:
