version: "3.9"

networks:
  kplan-net:
    driver: overlay
    attachable: true

volumes:
  rabbitmq_data:
  kplan_data:
    external: true
    name: kplan_data

configs:
  kplan_worker_conf:
    external: true

services:
  # -------------------------
  # PHP-FPM (application Symfony)
  # -------------------------
  php:
    image: ${KPLAN_IMAGE_PHP}
    depends_on:
      - redis
      - rabbitmq
    environment:
      APP_ROLE: web
      APP_ENV: ${APP_ENV:-prod}
      APP_DEBUG: ${APP_DEBUG:-0}
      MYSQL_HOST: ${DB_HOST}
      MYSQL_PORT: ${DB_PORT:-3306}
      MYSQL_DB_NAME: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_SERVER_VERSION: ${DB_SERVER_VERSION:-8.0}
      PHP_SESSION_SAVE_HANDLER: redis
      PHP_SESSION_SAVE_PATH: ${SESSION_HANDLER_DSN:-"tcp://redis:6379?database=2&prefix=sess:"}
      MESSENGER_TRANSPORT_DSN: ${MESSENGER_TRANSPORT_DSN}
      MESSENGER_TRANSPORT_DSN_HIGH: ${MESSENGER_TRANSPORT_DSN_HIGH}
      WKHTMLTOPDF_BINARY: /usr/local/bin/wkhtmltopdf
    networks:
      - kplan-net
    deploy:
      replicas: 2
      restart_policy:
        condition: on-failure
      update_config:
        order: start-first
        parallelism: 1
        delay: 0s

    healthcheck:
      test: ["CMD", "php", "-v"]  # simple et fiable
      interval: 30s
      timeout: 5s
      retries: 5
      start_period: 10s

  nginx:
    image: nginx:alpine
    ports:
      - target: 80
        published: 80
        protocol: tcp
        mode: host      # ou ingress si tu préfères les rolling updates
    networks:
      - kplan-net
    volumes:
      - /opt/kplan/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      - /opt/kplan/app/public:/var/www/public:ro
    deploy:
      restart_policy:
        condition: any
      update_config:
        order: stop-first  # utile en host-mode
    healthcheck:
      test: [ "CMD-SHELL", "wget -qO- http://localhost/healthz >/dev/null || exit 1" ]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s

  worker:
    image: ${KPLAN_IMAGE_PHP}
    depends_on:
      - redis
      - rabbitmq
    environment:
      APP_ROLE: worker
      APP_ENV: ${APP_ENV:-prod}
      APP_DEBUG: ${APP_DEBUG:-0}
      MYSQL_HOST: ${DB_HOST}
      MYSQL_PORT: ${DB_PORT:-3306}
      MYSQL_DB_NAME: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_SERVER_VERSION: ${DB_SERVER_VERSION:-8.0}
      PHP_SESSION_SAVE_HANDLER: redis
      PHP_SESSION_SAVE_PATH: ${SESSION_HANDLER_DSN:-"tcp://redis:6379?database=2&prefix=sess:"}
      MESSENGER_TRANSPORT_DSN: ${MESSENGER_TRANSPORT_DSN}
      MESSENGER_TRANSPORT_DSN_HIGH: ${MESSENGER_TRANSPORT_DSN_HIGH}
      WKHTMLTOPDF_BINARY: /usr/local/bin/wkhtmltopdf
    configs:
      - source: kplan_worker_conf
        target: /etc/supervisor/conf.d/worker.conf
        mode: 0444
    volumes:
      - kplan_data:/var/www/data
    networks:
      - kplan-net
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
      update_config:
        order: start-first
        parallelism: 1
        delay: 0s

  # -------------------------
  # Redis (sessions / cache)
  # -------------------------
  redis:
    image: redis:7-alpine
    networks:
      - kplan-net
    deploy:
      restart_policy:
        condition: on-failure

  # -------------------------
  # RabbitMQ (broker Messenger)
  # -------------------------
  rabbitmq:
    image: rabbitmq:3.13-management
    environment:
      RABBITMQ_DEFAULT_USER: ${RABBITMQ_USER:-kplan}
      RABBITMQ_DEFAULT_PASS: ${RABBITMQ_PASS:-kplanpass}
      RABBITMQ_DEFAULT_VHOST: ${RABBITMQ_VHOST:-/}
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
    ports:
      - "5672:5672"     # AMQP
      - "15672:15672"   # UI management
    networks:
      - kplan-net
    deploy:
      restart_policy:
        condition: on-failure

  # -------------------------
  # Dozzle (logs web)
  # -------------------------
  dozzle:
    image: amir20/dozzle:latest
    environment:
      DOZZLE_NO_ANALYTICS: "true"
    ports:
      - "9999:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    networks:
      - kplan-net
    deploy:
      restart_policy:
        condition: on-failure
