version: "3.9"

services:
  nats:
    image: nats:2.10
    command: ["-js", "-m", "8222"]
    ports:
      - "4222:4222"
      - "8222:8222"
    profiles: ["nats"]

  mqtt:
    image: eclipse-mosquitto:2.0
    ports:
      - "1883:1883"
      - "9001:9001"
    volumes:
      - ./mosquitto.conf:/mosquitto/config/mosquitto.conf:ro
    profiles: ["mqtt"]

  redis:
    image: redis:7.2
    ports:
      - "6379:6379"
    profiles: ["integration"]

  farmos-db:
    image: postgres:15
    environment:
      POSTGRES_DB: ${FARMOS_DB_NAME:-farmos}
      POSTGRES_USER: ${FARMOS_DB_USER:-farmos}
      POSTGRES_PASSWORD: ${FARMOS_DB_PASSWORD:-farmos}
    volumes:
      - farmos_db:/var/lib/postgresql/data
    profiles: ["farmos"]

  farmos:
    image: farmos/farmos:latest
    depends_on:
      - farmos-db
    environment:
      FARMOS_DB_HOST: farmos-db
      FARMOS_DB_NAME: ${FARMOS_DB_NAME:-farmos}
      FARMOS_DB_USER: ${FARMOS_DB_USER:-farmos}
      FARMOS_DB_PASSWORD: ${FARMOS_DB_PASSWORD:-farmos}
      FARMOS_BASE_URL: ${FARMOS_BASE_URL:-http://localhost:8080}
    profiles: ["farmos"]

  thingsboard:
    image: thingsboard/tb-postgres:latest
    ports:
      - "8081:8080"
    environment:
      TB_QUEUE_TYPE: ${TB_QUEUE_TYPE:-inmemory}
    profiles: ["thingsboard"]

  integration-service:
    build:
      context: ../../services/integration-service
      dockerfile: Dockerfile
    environment:
      PORT: ${INTEGRATION_PORT:-8800}
      LOG_LEVEL: ${LOG_LEVEL:-info}
      NATS_URL: ${NATS_URL:-nats://nats:4222}
      REDIS_URL: ${REDIS_URL:-redis://redis:6379}
      FARMOS_BASE_URL: ${FARMOS_BASE_URL:-http://farmos}
      FARMOS_TOKEN: ${FARMOS_TOKEN:-}
      FARMOS_USER: ${FARMOS_USER:-}
      FARMOS_PASSWORD: ${FARMOS_PASSWORD:-}
      THINGSBOARD_BASE_URL: ${THINGSBOARD_BASE_URL:-http://thingsboard:8080}
      THINGSBOARD_TOKEN: ${THINGSBOARD_TOKEN:-}
      AGX_HMAC_SECRET: ${AGX_HMAC_SECRET:-}
      AGX_HMAC_REQUIRED: ${AGX_HMAC_REQUIRED:-true}
      AGX_HMAC_WINDOW_SECONDS: ${AGX_HMAC_WINDOW_SECONDS:-300}
      AGX_RATE_LIMIT_PER_MIN: ${AGX_RATE_LIMIT_PER_MIN:-120}
      DLQ_ENABLED: ${DLQ_ENABLED:-true}
      MAPPING_PATH: ${MAPPING_PATH:-/app/config/mappings.json}
    ports:
      - "8800:8800"
    depends_on:
      - nats
      - redis
      - farmos
    volumes:
      - integration_data:/data
    profiles: ["integration"]

  farmos_ui_proxy:
    image: caddy:2
    depends_on:
      - farmos
    environment:
      FARMOS_UPSTREAM: ${FARMOS_UPSTREAM:-http://farmos}
      FARMOS_UI_USER: ${FARMOS_UI_USER:-admin}
      FARMOS_UI_PASS_HASH: ${FARMOS_UI_PASS_HASH:-}
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
    ports:
      - "127.0.0.1:${FARMOS_UI_PORT:-18080}:8080"
    profiles: ["farmos_ui"]

  litefarm:
    image: litefarmorg/litefarm:latest
    profiles: ["litefarm"]

  tania:
    image: usetania/tania-core:latest
    profiles: ["tania"]

volumes:
  farmos_db:
  integration_data:
