version: "3.8"

services:
  billmora-app:
    image: ghcr.io/billmora/billmora:latest
    restart: always
    environment:
      - APP_NAME=${APP_NAME}
      - APP_ENV=${APP_ENV}
      - APP_KEY=${APP_KEY}
      - APP_URL=${APP_URL}
      - DB_CONNECTION=${DB_CONNECTION}
      - DB_HOST=${DB_HOST}
      - DB_PORT=${DB_PORT}
      - DB_DATABASE=${DB_DATABASE}
      - DB_USERNAME=${DB_USERNAME}
      - DB_PASSWORD=${DB_PASSWORD}
      - REDIS_HOST=${REDIS_HOST}
      - REDIS_PASSWORD=${REDIS_PASSWORD}
      - REDIS_PORT=${REDIS_PORT}
      - SESSION_DRIVER=${SESSION_DRIVER}
      - CACHE_STORE=${CACHE_STORE}
      - QUEUE_CONNECTION=${QUEUE_CONNECTION}
      - AUTORUN_ENABLED=true
      - AUTORUN_LARAVEL_MIGRATION=true
      - AUTORUN_LARAVEL_MIGRATION_FORCE=true
      - AUTORUN_LARAVEL_MIGRATION_SEED=true
      - AUTORUN_LARAVEL_STORAGE_LINK=true
    volumes:
      - billmora-storage:/var/www/html/storage
    depends_on:
      billmora-db:
        condition: service_healthy
      billmora-redis:
        condition: service_healthy

  billmora-worker:
    image: ghcr.io/billmora/billmora:latest
    restart: always
    command: ["php", "artisan", "queue:work", "--sleep=3", "--tries=3", "--max-time=3600"]
    environment:
      - APP_NAME=${APP_NAME}
      - APP_ENV=${APP_ENV}
      - APP_KEY=${APP_KEY}
      - APP_URL=${APP_URL}
      - DB_CONNECTION=${DB_CONNECTION}
      - DB_HOST=${DB_HOST}
      - DB_PORT=${DB_PORT}
      - DB_DATABASE=${DB_DATABASE}
      - DB_USERNAME=${DB_USERNAME}
      - DB_PASSWORD=${DB_PASSWORD}
      - REDIS_HOST=${REDIS_HOST}
      - REDIS_PASSWORD=${REDIS_PASSWORD}
      - REDIS_PORT=${REDIS_PORT}
      - SESSION_DRIVER=${SESSION_DRIVER}
      - CACHE_STORE=${CACHE_STORE}
      - QUEUE_CONNECTION=${QUEUE_CONNECTION}
      - AUTORUN_ENABLED=false
    volumes:
      - billmora-storage:/var/www/html/storage
    depends_on:
      billmora-db:
        condition: service_healthy
      billmora-redis:
        condition: service_healthy

  billmora-scheduler:
    image: ghcr.io/billmora/billmora:latest
    restart: always
    command: ["/bin/sh", "-c", "while true; do php artisan schedule:run >> /dev/null 2>&1; sleep 60; done"]
    environment:
      - APP_NAME=${APP_NAME}
      - APP_ENV=${APP_ENV}
      - APP_KEY=${APP_KEY}
      - APP_URL=${APP_URL}
      - DB_CONNECTION=${DB_CONNECTION}
      - DB_HOST=${DB_HOST}
      - DB_PORT=${DB_PORT}
      - DB_DATABASE=${DB_DATABASE}
      - DB_USERNAME=${DB_USERNAME}
      - DB_PASSWORD=${DB_PASSWORD}
      - REDIS_HOST=${REDIS_HOST}
      - REDIS_PASSWORD=${REDIS_PASSWORD}
      - REDIS_PORT=${REDIS_PORT}
      - SESSION_DRIVER=${SESSION_DRIVER}
      - CACHE_STORE=${CACHE_STORE}
      - QUEUE_CONNECTION=${QUEUE_CONNECTION}
      - AUTORUN_ENABLED=false
    volumes:
      - billmora-storage:/var/www/html/storage
    depends_on:
      billmora-db:
        condition: service_healthy
      billmora-redis:
        condition: service_healthy

  billmora-db:
    image: mariadb:11
    restart: always
    environment:
      - MARIADB_DATABASE=${DB_DATABASE}
      - MARIADB_USER=${DB_USERNAME}
      - MARIADB_PASSWORD=${DB_PASSWORD}
      - MARIADB_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
    volumes:
      - billmora-db-data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s

  billmora-redis:
    image: redis:7-alpine
    restart: always
    command: redis-server --requirepass ${REDIS_PASSWORD}
    volumes:
      - billmora-redis-data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "--no-auth-warning", "-a", "${REDIS_PASSWORD}", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 10s

volumes:
  billmora-storage:
  billmora-db-data:
  billmora-redis-data:
