# Clusev — DEV stack. Host has only Docker; everything runs here.
# Tooling:  docker compose run --rm --no-deps -u "${HOST_UID:-1002}:${HOST_GID:-1002}" app <composer|php artisan|npm ...>
name: clusev

x-app: &app
  build:
    context: .
    dockerfile: Dockerfile
    target: dev
    args:
      APP_UID: "${HOST_UID:-1002}"
      APP_GID: "${HOST_GID:-1002}"
  image: clusev-app:dev
  restart: unless-stopped
  volumes:
    - .:/var/www/html
  networks:
    - clusev

services:
  # php-fpm + nginx + vite, all via supervisor in ONE container.
  # Vite is NOT a separate service — it comes up with the app (HMR on :5173).
  app:
    <<: *app
    ports:
      - "${APP_PORT:-80}:80"
      - "${VITE_PORT:-5173}:5173"
    depends_on:
      mariadb:
        condition: service_healthy
      redis:
        condition: service_started

  reverb:
    <<: *app
    user: "${HOST_UID:-1002}:${HOST_GID:-1002}"
    command: php artisan reverb:start --host=0.0.0.0 --port=8080
    ports:
      - "${REVERB_HOST_PORT:-8080}:8080"
    depends_on:
      redis:
        condition: service_started

  queue:
    <<: *app
    user: "${HOST_UID:-1002}:${HOST_GID:-1002}"
    command: php artisan queue:work --tries=3 --timeout=90 --sleep=1
    depends_on:
      mariadb:
        condition: service_healthy
      redis:
        condition: service_started

  # Laravel scheduler loop (every-minute clusev:wg-sample, daily clusev:prune-audit).
  scheduler:
    <<: *app
    user: "${HOST_UID:-1002}:${HOST_GID:-1002}"
    command: php artisan schedule:work
    depends_on:
      mariadb:
        condition: service_healthy
      redis:
        condition: service_started

  mariadb:
    image: mariadb:11
    restart: unless-stopped
    environment:
      MARIADB_DATABASE: "${DB_DATABASE:-clusev}"
      MARIADB_USER: "${DB_USERNAME:-clusev}"
      MARIADB_PASSWORD: "${DB_PASSWORD:?set DB_PASSWORD in .env}"
      MARIADB_ROOT_PASSWORD: "${DB_ROOT_PASSWORD:?set DB_ROOT_PASSWORD in .env}"
    volumes:
      - mariadb-data:/var/lib/mysql
    ports:
      # localhost-only: reachable from the host (DB tools / SSH tunnel), not the whole network.
      - "127.0.0.1:${DB_PORT:-3306}:3306"
    healthcheck:
      test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
      interval: 5s
      timeout: 5s
      retries: 30
    networks:
      - clusev

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    command: redis-server --appendonly yes
    volumes:
      - redis-data:/data
    networks:
      - clusev

volumes:
  mariadb-data:
  redis-data:

networks:
  clusev:
    driver: bridge
