services:
  # --- Infrastructure ---

  kafka:
    image: apache/kafka:3.7.0
    environment:
      KAFKA_NODE_ID: 1
      KAFKA_PROCESS_ROLES: broker,controller
      KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092,CONTROLLER://0.0.0.0:9093
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
      KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
      KAFKA_CONTROLLER_QUORUM_VOTERS: 1@kafka:9093
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
      CLUSTER_ID: drakkar-integration-test
    ports:
      - "9092:9092"
    healthcheck:
      test: ["CMD-SHELL", "/opt/kafka/bin/kafka-broker-api-versions.sh --bootstrap-server=localhost:9092"]
      interval: 5s
      timeout: 10s
      retries: 15
      start_period: 10s

  kafka-init:
    image: apache/kafka:3.7.0
    depends_on:
      kafka:
        condition: service_healthy
    volumes:
      - ./infra/init-kafka.sh:/init-kafka.sh
    entrypoint: ["/bin/bash", "/init-kafka.sh"]

  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: drakkar
      POSTGRES_PASSWORD: drakkar
      POSTGRES_DB: drakkar
    ports:
      - "5432:5432"
    volumes:
      - ./infra/init-db.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U drakkar"]
      interval: 3s
      timeout: 5s
      retries: 10

  mongo:
    image: mongo:7
    ports:
      - "27017:27017"
    healthcheck:
      test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
      interval: 5s
      timeout: 5s
      retries: 10

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 3s
      timeout: 3s
      retries: 10

  webhook:
    image: mendhak/http-https-echo:latest
    ports:
      - "9000:8080"
    environment:
      HTTP_PORT: 8080

  # --- Web UIs ---

  kafka-ui:
    image: provectuslabs/kafka-ui:latest
    depends_on:
      kafka:
        condition: service_healthy
    ports:
      - "8088:8080"
    environment:
      DYNAMIC_CONFIG_ENABLED: "true"
      KAFKA_CLUSTERS_0_NAME: drakkar-local
      KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:9092

  mongo-express:
    image: mongo-express:latest
    depends_on:
      mongo:
        condition: service_healthy
    ports:
      - "8089:8081"
    environment:
      ME_CONFIG_MONGODB_URL: mongodb://mongo:27017
      ME_CONFIG_BASICAUTH: "false"

  redis-commander:
    image: rediscommander/redis-commander:latest
    depends_on:
      redis:
        condition: service_healthy
    ports:
      - "8087:8081"
    environment:
      REDIS_HOSTS: local:redis:6379

  prometheus:
    image: prom/prometheus:latest
    ports:
      - "9099:9090"
    volumes:
      - ./infra/prometheus.yml:/etc/prometheus/prometheus.yml:ro
    depends_on:
      kafka-init:
        condition: service_completed_successfully

  # --- Drakkar Workers ---

  worker-1:
    build:
      context: ..
      dockerfile: integration/worker/Dockerfile
    depends_on:
      kafka-init:
        condition: service_completed_successfully
      postgres:
        condition: service_healthy
      mongo:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      WORKER_ID: worker-1
      DRAKKAR_CONFIG: /app/drakkar.yaml
      DRAKKAR_METRICS__PORT: "9090"
      DRAKKAR_DEBUG__PORT: "8081"
      DRAKKAR_DEBUG__DEBUG_URL: "http://localhost:8081/"
    ports:
      - "9090:9090"
      - "8081:8081"
    volumes:
      - ..:/project:ro
      - ./shared:/shared

  worker-2:
    build:
      context: ..
      dockerfile: integration/worker/Dockerfile
    depends_on:
      kafka-init:
        condition: service_completed_successfully
      postgres:
        condition: service_healthy
      mongo:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      WORKER_ID: worker-2
      DRAKKAR_CONFIG: /app/drakkar.yaml
      DRAKKAR_METRICS__PORT: "9091"
      DRAKKAR_DEBUG__PORT: "8082"
      DRAKKAR_DEBUG__DEBUG_URL: "http://localhost:8082/"
    ports:
      - "9091:9091"
      - "8082:8082"
    volumes:
      - ..:/project:ro
      - ./shared:/shared

  worker-3:
    build:
      context: ..
      dockerfile: integration/worker/Dockerfile
    depends_on:
      kafka-init:
        condition: service_completed_successfully
      postgres:
        condition: service_healthy
      mongo:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      WORKER_ID: worker-3
      DRAKKAR_CONFIG: /app/drakkar.yaml
      DRAKKAR_METRICS__PORT: "9093"
      DRAKKAR_DEBUG__PORT: "8083"
      DRAKKAR_DEBUG__DEBUG_URL: "http://localhost:8083/"
    ports:
      - "9093:9093"
      - "8083:8083"
    volumes:
      - ..:/project:ro
      - ./shared:/shared

  # --- Fast Workers (symbol counting) ---

  fast-worker-1:
    build:
      context: ..
      dockerfile: integration/fast-worker/Dockerfile
    depends_on:
      kafka-init:
        condition: service_completed_successfully
    environment:
      WORKER_ID: fast-worker-1
      DRAKKAR_CONFIG: /app/drakkar.yaml
      DRAKKAR_METRICS__PORT: "9094"
      DRAKKAR_DEBUG__PORT: "8084"
      DRAKKAR_DEBUG__DEBUG_URL: "http://localhost:8084/"
    ports:
      - "9094:9094"
      - "8084:8084"
    volumes:
      - ..:/project:ro
      - ./shared:/shared

  fast-worker-2:
    build:
      context: ..
      dockerfile: integration/fast-worker/Dockerfile
    depends_on:
      kafka-init:
        condition: service_completed_successfully
    environment:
      WORKER_ID: fast-worker-2
      DRAKKAR_CONFIG: /app/drakkar.yaml
      DRAKKAR_METRICS__PORT: "9095"
      DRAKKAR_DEBUG__PORT: "8085"
      DRAKKAR_DEBUG__DEBUG_URL: "http://localhost:8085/"
    ports:
      - "9095:9095"
      - "8085:8085"
    volumes:
      - ..:/project:ro
      - ./shared:/shared

  # --- Message Producer ---

  producer:
    build:
      context: ..
      dockerfile: integration/infra/Dockerfile.producer
    depends_on:
      kafka-init:
        condition: service_completed_successfully
    environment:
      KAFKA_BROKERS: kafka:9092
      SOURCE_TOPIC: search-requests
      TOTAL_MESSAGES: "5000"
