version: "3.8"

services:
  mysql:
    image: mysql:8.0
    container_name: mysql
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=rootpass
      - MYSQL_DATABASE=lambdaiot
    volumes:
      - ../ai-improved.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-prootpass"]
      interval: 2s
      timeout: 2s
      retries: 10

  mosquitto:
    image: eclipse-mosquitto:2.0
    container_name: mosquitto
    ports:
      - "1883:1883"
    volumes:
      - mosquitto_data:/mosquitto/data
      - mosquitto_log:/mosquitto/log
      - ./mosquitto.conf:/mosquitto/config/mosquitto.conf
      - ./aclfile:/mosquitto/data/aclfile:ro
    healthcheck:
      test: ["CMD", "mosquitto", "--help"]
      interval: 2s
      timeout: 2s
      retries: 10
    environment:
      - MOSQ_USER=testuser
      - MOSQ_PASS=testpass
    # ensure a password file exists (in the writable data volume) and start mosquitto with our config
    command: sh -c "mkdir -p /mosquitto/data; mosquitto_passwd -c -b /mosquitto/data/passwordfile ${MOSQ_USER} ${MOSQ_PASS} >/dev/null 2>&1 || true; chown -R mosquitto:mosquitto /mosquitto/data || true; /usr/sbin/mosquitto -c /mosquitto/config/mosquitto.conf"
  server:
    build:
      context: ..
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    environment:
      - SERVER_ADDRESS=0.0.0.0
      - SERVER_PORT=8080
      - JWT_SECRET=secret
      - MQTT_BROKER=tcp://mosquitto:1883
      - MQTT_CLIENT_ID=lambdaiot-server
      - MQTT_TOPIC=lambdaiot
      - MQTT_USERNAME=testuser
      - MQTT_PASSWORD=testpass
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_USER=root
      - DB_PASSWORD=rootpass
    depends_on:
      - mysql
      - mosquitto
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 10s
      timeout: 5s
      retries: 5

    # no command override needed

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    ports:
      - "8081:80"
    environment:
      - PMA_HOST=mysql
      - PMA_PORT=3306
      - PMA_USER=root
      - PMA_PASSWORD=rootpass
    depends_on:
      - mysql

volumes:
  mosquitto_data:
  mosquitto_log:

networks:
  default:
    name: lambdaiot_test_net
