# {{projectName}} infra docker compose — MariaDB (binlog CDC) + MinIO.
#
# Brought up by `pnpm db:up`. Apps run natively via `pnpm dev` against
# this. For full-docker dev use `pnpm dev:docker`.
#
# MariaDB on 3307 (NOT default 3306) so it never clashes with a host
# MySQL/MariaDB. The binlog flags are MANDATORY for Voltro's CDC reader:
# real-time subscriptions fan out across replicas by tailing the binary
# log (the binlog IS the message bus — no Redis/NATS).

services:
  mariadb:
    image: mariadb:11
    container_name: {{projectName}}-mariadb
    restart: unless-stopped
    environment:
      MARIADB_ROOT_PASSWORD: root
      MARIADB_DATABASE: {{projectNameSnake}}
      MARIADB_USER: app
      MARIADB_PASSWORD: app
    ports:
      - "3307:3306"
    command:
      # CDC requirements — ROW format + FULL row image so the reader sees
      # complete before/after images; a server_id (unique per replica in
      # prod — see the Helm baseline); generous binlog retention.
      - "--log-bin=mysql-bin"
      - "--binlog-format=ROW"
      - "--binlog-row-image=FULL"
      - "--server-id=1"
      - "--binlog-expire-logs-seconds=86400"
    volumes:
      - {{projectName}}-mariadbdata:/var/lib/mysql
      # Grants the app user REPLICATION SLAVE/CLIENT (the binlog reader needs it).
      - ./docker/mariadb-init.sql:/docker-entrypoint-initdb.d/10-replication.sql:ro
    healthcheck:
      test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
      interval: 2s
      timeout: 2s
      retries: 15

  # Object storage for the file-storage primitive (@voltro/plugin-storage).
  minio:
    image: minio/minio
    container_name: {{projectName}}-minio
    restart: unless-stopped
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin
    ports:
      - "9000:9000"   # S3 API
      - "9001:9001"   # console
    command: server /data --console-address ":9001"
    volumes:
      - {{projectName}}-miniodata:/data
    healthcheck:
      test: ["CMD", "mc", "ready", "local"]
      interval: 3s
      timeout: 2s
      retries: 15

  # One-shot: create the app bucket + make it publicly readable (avatars
  # are served via the public /_voltro/storage/:id route).
  createbuckets:
    image: minio/mc
    depends_on:
      minio:
        condition: service_healthy
    entrypoint: >
      /bin/sh -c "
      mc alias set local http://minio:9000 minioadmin minioadmin &&
      mc mb -p local/{{projectNameSnake}} &&
      mc anonymous set download local/{{projectNameSnake}};
      exit 0;
      "

volumes:
  {{projectName}}-mariadbdata:
  {{projectName}}-miniodata:
