services:

  # ── Init: копирует web/ из образа в shared volume (нужен nginx) ─────────
  web_init:
    image: breadcrumb-the-shire:latest
    volumes:
      - web_assets:/target
    command: sh -c "cp -rp /var/www/web/. /target/ && echo 'Web assets OK'"
    restart: "no"

  # ── nginx: отдаёт статику + FastCGI → php ───────────────────────────────
  nginx:
    image: nginx:1.25-alpine
    ports:
      - "127.0.0.1:8084:80"   # host nginx проксирует сюда
    volumes:
      - ./nginx/site.conf:/etc/nginx/conf.d/default.conf:ro
      - web_assets:/var/www/web:ro
    depends_on:
      web_init:
        condition: service_completed_successfully
      php:
        condition: service_started
    restart: unless-stopped

  # ── PHP-FPM: весь код внутри образа ─────────────────────────────────────
  php:
    image: breadcrumb-the-shire:latest
    env_file: .env
    volumes:
      - ./storage:/var/www/storage
      - ./var:/var/www/var
    depends_on:
      - db
      - memcached
    restart: unless-stopped

  # ── Планировщик задач ────────────────────────────────────────────────────
  scheduler:
    image: breadcrumb-the-shire:latest
    env_file: .env
    volumes:
      - ./storage:/var/www/storage
      - ./var:/var/www/var
    depends_on:
      - db
      - memcached
    command: sh -c "while true; do php -d display_errors=0 bin/console scheduler:run || true; sleep 60; done"
    restart: unless-stopped

  # ── MariaDB ──────────────────────────────────────────────────────────────
  db:
    image: mariadb:11.4
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    env_file: .env
    environment:
      MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MARIADB_DATABASE: ${DB_NAME}
      MARIADB_USER: ${DB_USER}
      MARIADB_PASSWORD: ${DB_PASS}
    volumes:
      - db_data:/var/lib/mysql
    restart: unless-stopped

  # ── Memcached ────────────────────────────────────────────────────────────
  memcached:
    image: memcached:1.6-alpine
    restart: unless-stopped

volumes:
  web_assets:   # статика из образа (web/)
  db_data:      # данные MariaDB
