version: '3.8'

services:
###> doctrine/doctrine-bundle ###
  database:
    image: mysql:${MYSQL_VERSION:-8.0}
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-root}
      MYSQL_DATABASE: ${MYSQL_DATABASE:-HRapp}
      MYSQL_USER: ${MYSQL_USER:-root}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD:-}
    ports:
      - "${MYSQL_PORT:-3308}:3306"
    volumes:
      - db-data:/var/lib/mysql:rw
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5
###< doctrine/doctrine-bundle ###

###> redis cache ###
  redis:
    image: redis:${REDIS_VERSION:-7}-alpine
    ports:
      - "${REDIS_PORT:-6379}:6379"
    volumes:
      - redis-data:/data
    command: redis-server --appendonly yes
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3
###< redis cache ###

###> app service ###
  # PHP application (built from local Dockerfile).
  # Run:  docker compose build app  then  docker compose up app
  app:
    build:
      context: .
      target: app
    image: erpstarz-app:latest
    environment:
      APP_ENV: "${APP_ENV:-prod}"
      APP_SECRET: "${APP_SECRET}"
      DATABASE_URL: "${DATABASE_URL}"
      REDIS_URL: "${REDIS_URL:-redis://redis:6379}"
      MESSENGER_TRANSPORT_DSN: "${MESSENGER_TRANSPORT_DSN:-doctrine://default}"
    volumes:
      - ./.env.local:/app/.env.local:ro
      - app-var:/app/var
    depends_on:
      database:
        condition: service_healthy
      redis:
        condition: service_healthy
    ports:
      - "${APP_PORT:-8000}:8000"
    command: ["php", "-S", "0.0.0.0:8000", "-t", "public"]
    restart: unless-stopped
###< app service ###

###> messenger worker ###
  # Async Messenger consumer — processes emails and background jobs.
  # Requires the app image to be built first: docker compose build app
  messenger_worker:
    image: erpstarz-app:latest
    environment:
      APP_ENV: "${APP_ENV:-prod}"
      APP_SECRET: "${APP_SECRET}"
      DATABASE_URL: "${DATABASE_URL}"
      REDIS_URL: "${REDIS_URL:-redis://redis:6379}"
      MESSENGER_TRANSPORT_DSN: "${MESSENGER_TRANSPORT_DSN:-doctrine://default}"
    volumes:
      - ./.env.local:/app/.env.local:ro
      - app-var:/app/var
    depends_on:
      database:
        condition: service_healthy
      redis:
        condition: service_healthy
    command: ["php", "bin/console", "messenger:consume", "async", "--time-limit=3600", "--memory-limit=256M", "--no-debug"]
    restart: unless-stopped
###< messenger worker ###

volumes:
###> doctrine/doctrine-bundle ###
  db-data:
###< doctrine/doctrine-bundle ###
  redis-data:
  app-var:

