# IoT Agent JSON - 3 instances (one per MQTT broker)
# Usage: docker compose -f docker-compose.yml -f docker-compose.iot-agent.yml up -d
version: '3.8'

services:
  # Instance 1: EMQX
  iot-agent-emqx:
    image: fiware/iotagent-json:latest
    container_name: smart-city-iot-agent-emqx
    restart: unless-stopped
    networks:
      - smartcity-shared
      - traefik-public
    ports:
      - "4041:4041"
    environment:
      # Context Broker (Orion-LD)
      - IOTA_CB_HOST=smart-city-orion-ld
      - IOTA_CB_PORT=1026
      - IOTA_CB_NGSI_VERSION=v2
      # IoT Agent settings
      - IOTA_NORTH_PORT=4041
      - IOTA_REGISTRY_TYPE=memory
      # MQTT Listener - EMQX
      - IOTA_MQTT_HOST=emqx_emqx_1
      - IOTA_MQTT_PORT=1883
      - IOTA_PROVIDER_URL=http://smart-city-iot-agent-emqx:4041
      - IOTA_DEFAULT_RESOURCE=/
      - IOTA_DEFAULT_APIKEY=smartcity-emqx
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.iot-agent-emqx.rule=Host(`iot-agent-emqx.digitribe.fr`)"
      - "traefik.http.routers.iot-agent-emqx.entrypoints=websecure"
      - "traefik.http.routers.iot-agent-emqx.tls=true"
      - "traefik.http.services.iot-agent-emqx.loadbalancer.server.port=4041"

  # Instance 2: Mosquitto
  iot-agent-mosquitto:
    image: fiware/iotagent-json:latest
    container_name: smart-city-iot-agent-mosquitto
    restart: unless-stopped
    networks:
      - smartcity-shared
    ports:
      - "4042:4042"
    environment:
      # Context Broker (Orion-LD)
      - IOTA_CB_HOST=smart-city-orion-ld
      - IOTA_CB_PORT=1026
      - IOTA_CB_NGSI_VERSION=v2
      # IoT Agent settings
      - IOTA_NORTH_PORT=4042
      - IOTA_REGISTRY_TYPE=memory
      # MQTT Listener - Mosquitto
      - IOTA_MQTT_HOST=mosquitto
      - IOTA_MQTT_PORT=1883
      - IOTA_PROVIDER_URL=http://smart-city-iot-agent-mosquitto:4042
      - IOTA_DEFAULT_RESOURCE=/
      - IOTA_DEFAULT_APIKEY=smartcity-mosquitto

  # MongoDB for IoT Agents
  iot-mongodb:
    image: mongo:4.4
    container_name: smart-city-iot-mongodb
    restart: unless-stopped
    networks:
      - smartcity-shared
    ports:
      - "27017:27017"
    volumes:
      - iot-mongodb-data:/data/db
    healthcheck:
      test: ["CMD", "mongo", "--eval", "db.adminCommand('ping')"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Instance3: BunkerM (Stellio NGSI-LD)
  iot-agent-bunkerm:
    image: fiware/iotagent-json:latest
    container_name: smart-city-iot-agent-bunkerm
    restart: unless-stopped
    networks:
      - smartcity-shared
    ports:
      - "4043:4043"
    environment:
      # Context Broker (Stellio NGSI-LD)
      - IOTA_CB_HOST=stellio-api-gateway
      - IOTA_CB_PORT=8080
      - IOTA_CB_NGSI_VERSION=ld
      # IoT Agent settings
      - IOTA_NORTH_PORT=4043
      - IOTA_REGISTRY_TYPE=memory
      # MQTT Listener - BunkerM
      - IOTA_MQTT_HOST=bunkerm_bunkerm_1
      - IOTA_MQTT_PORT=1900
      - IOTA_MQTT_USERNAME=bunker
      - IOTA_MQTT_PASSWORD=bunker
      - IOTA_PROVIDER_URL=http://smart-city-iot-agent-bunkerm:4043
      - IOTA_DEFAULT_RESOURCE=/
      - IOTA_DEFAULT_APIKEY=smartcity-bunkerm

networks:
  smartcity-shared:
    external: true
  traefik-public:
    external: true

volumes:
  iot-mongodb-data:
    external: true
    name: smart-city-digital-twin-martinique_iot-mongodb-data
