version: "2"
services:
  kafdrop:
    image: obsidiandynamics/kafdrop
    restart: "no"
    ports:
      - "127.0.0.1:9000:9000"
    environment:
      KAFKA_BROKERCONNECT: "kafka:29092"
    depends_on:
      - "kafka"
  kafka:
    image: apache/kafka:3.9.0
    restart: "no"
    ports:
      # - "127.0.0.1:2181:2181"
      - "127.0.0.1:9092:9092"
      - "127.0.0.1:9093:9093"
    healthcheck: # <-- ADD THIS BLOCK
      test: ["CMD-SHELL", "/opt/kafka/bin/kafka-topics.sh --bootstrap-server 127.0.0.1:9092 --list"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 20s
    environment:
      KAFKA_NODE_ID: 1
      KAFKA_PROCESS_ROLES: broker,controller      
      KAFKA_LISTENERS: INTERNAL://:29092,EXTERNAL://:9092,CONTROLLER://127.0.0.1:9093
      KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka:29092,EXTERNAL://localhost:9092
      KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT
      KAFKA_CONTROLLER_QUORUM_VOTERS: 1@127.0.0.1:9093
      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
      KAFKA_NUM_PARTITIONS: 3

  kafka-init:
    image: apache/kafka:3.9.0
    command: [ "/bin/bash", "-c", "chmod +x /tmp/create_topic.sh && /tmp/create_topic.sh"]
    depends_on:
      kafka:
        condition: service_healthy
    volumes:
      - ./init-scripts/create_topic.sh:/tmp/create_topic.sh
    environment:
      - TOPIC_NAMES=topic1,topic2,topic3
  
  valkey:
    image: valkey/valkey:9.0.0
    container_name: valkey
    ports:
      - "127.0.0.1:6379:6379"

  presense-decoder:
    build:
      context: ../
      dockerfile: build/package/Dockerfile.decoder
    image: presense-decoder
    container_name: presense-decoder
    environment:
      - KAFKA_URL=kafka:29092
    depends_on:
      - kafka-init
    restart: always
  
  presense-server:
    build:
      context: ../
      dockerfile: build/package/Dockerfile.server
    image: presense-server
    container_name: presense-server
    environment:
      - VALKEY_URL=valkey:6379
      - KAFKA_URL=kafka:29092
    ports:
      - "127.0.0.1:1902:1902"
    depends_on:
      - kafka-init
      - valkey
    restart: always

  presense-bridge:
    build:
      context: ../
      dockerfile: build/package/Dockerfile.bridge
    image: presense-bridge
    container_name: presense-bridge
    environment:
      - KAFKA_URL=kafka:29092
      - MQTT_HOST=192.168.1.101:1883
      - MQTT_USERNAME=user
      - MQTT_PASSWORD=pass
    depends_on:
      - kafka-init
    restart: always

  presense-location:
    build:
      context: ../
      dockerfile: build/package/Dockerfile.location
    image: presense-location
    container_name: presense-location
    environment:
      - KAFKA_URL=kafka:29092
    depends_on:
      - kafka-init
    restart: always
      


