x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

services:
  # Zookeeper for Kafka coordination
  zookeeper:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}confluentinc/cp-zookeeper:${KAFKA_VERSION:-7.8.0}
    ports:
      - '${ZOOKEEPER_CLIENT_PORT_OVERRIDE:-2181}:2181'
    volumes:
      - zookeeper_data:/var/lib/zookeeper/data
      - zookeeper_log:/var/lib/zookeeper/log
    environment:
      - TZ=${TZ:-UTC}
      - ZOOKEEPER_CLIENT_PORT=2181
      - ZOOKEEPER_TICK_TIME=2000
      - ZOOKEEPER_SYNC_LIMIT=5
      - ZOOKEEPER_INIT_LIMIT=10
      - ZOOKEEPER_MAX_CLIENT_CNXNS=60
      - ZOOKEEPER_AUTOPURGE_SNAP_RETAIN_COUNT=3
      - ZOOKEEPER_AUTOPURGE_PURGE_INTERVAL=24
    deploy:
      resources:
        limits:
          cpus: ${ZOOKEEPER_CPU_LIMIT:-1.00}
          memory: ${ZOOKEEPER_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${ZOOKEEPER_CPU_RESERVATION:-0.25}
          memory: ${ZOOKEEPER_MEMORY_RESERVATION:-256M}
    healthcheck:
      test: [CMD-SHELL, 'echo ruok | nc localhost 2181 | grep imok']
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s

  # Kafka broker
  kafka:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}confluentinc/cp-kafka:${KAFKA_VERSION:-7.8.0}
    depends_on:
      zookeeper:
        condition: service_healthy
    ports:
      - '${KAFKA_BROKER_PORT_OVERRIDE:-9092}:9092'
      - '${KAFKA_JMX_PORT_OVERRIDE:-9999}:9999'
    volumes:
      - kafka_data:/var/lib/kafka/data
    environment:
      - TZ=${TZ:-UTC}
      - KAFKA_BROKER_ID=1
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
      - KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092
      - KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT
      - KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT
      - KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1
      - KAFKA_TRANSACTION_STATE_LOG_MIN_ISR=1
      - KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR=1
      - KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS=0
      - KAFKA_NUM_PARTITIONS=${KAFKA_NUM_PARTITIONS:-3}
      - KAFKA_DEFAULT_REPLICATION_FACTOR=${KAFKA_DEFAULT_REPLICATION_FACTOR:-1}
      - KAFKA_AUTO_CREATE_TOPICS_ENABLE=${KAFKA_AUTO_CREATE_TOPICS_ENABLE:-true}
      - KAFKA_DELETE_TOPIC_ENABLE=${KAFKA_DELETE_TOPIC_ENABLE:-true}
      - KAFKA_LOG_RETENTION_HOURS=${KAFKA_LOG_RETENTION_HOURS:-168}
      - KAFKA_LOG_SEGMENT_BYTES=${KAFKA_LOG_SEGMENT_BYTES:-1073741824}
      - KAFKA_LOG_RETENTION_CHECK_INTERVAL_MS=300000
      - KAFKA_JMX_PORT=9999
      - KAFKA_JMX_HOSTNAME=localhost
      - KAFKA_HEAP_OPTS=${KAFKA_HEAP_OPTS:--Xmx1G -Xms1G}
    deploy:
      resources:
        limits:
          cpus: ${KAFKA_CPU_LIMIT:-2.00}
          memory: ${KAFKA_MEMORY_LIMIT:-2G}
        reservations:
          cpus: ${KAFKA_CPU_RESERVATION:-0.50}
          memory: ${KAFKA_MEMORY_RESERVATION:-1G}
    healthcheck:
      test:
        - CMD-SHELL
        - 'kafka-broker-api-versions --bootstrap-server localhost:9092'

      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s

  # Kafka UI (optional)
  kafka-ui:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}provectuslabs/kafka-ui:${KAFKA_UI_VERSION:-latest}
    profiles:
      - ui
    depends_on:
      kafka:
        condition: service_healthy
      zookeeper:
        condition: service_healthy
    ports:
      - '${KAFKA_UI_PORT_OVERRIDE:-8080}:8080'
    environment:
      - TZ=${TZ:-UTC}
      - KAFKA_CLUSTERS_0_NAME=local
      - KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=kafka:9092
      - KAFKA_CLUSTERS_0_ZOOKEEPER=zookeeper:2181
      - KAFKA_CLUSTERS_0_READONLY=${KAFKA_UI_READONLY:-false}
    deploy:
      resources:
        limits:
          cpus: ${KAFKA_UI_CPU_LIMIT:-0.50}
          memory: ${KAFKA_UI_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${KAFKA_UI_CPU_RESERVATION:-0.10}
          memory: ${KAFKA_UI_MEMORY_RESERVATION:-128M}

volumes:
  zookeeper_data:
  zookeeper_log:
  kafka_data:
