name: meet-meals-backend

x-app-env: &app-env
  APP_NAME: ${APP_NAME:-Meet Meals}
  APP_ENV: production
  APP_KEY: ${APP_KEY}
  APP_DEBUG: "false"
  APP_URL: ${APP_URL}
  LOG_CHANNEL: stderr
  LOG_LEVEL: ${LOG_LEVEL:-info}

  DB_CONNECTION: mysql
  DB_HOST: db
  DB_PORT: "3306"
  DB_DATABASE: ${DB_DATABASE:-meet_meals}
  DB_USERNAME: ${DB_USERNAME:-meet_meals}
  DB_PASSWORD: ${DB_PASSWORD}

  SESSION_DRIVER: database
  QUEUE_CONNECTION: database
  CACHE_STORE: database
  FILESYSTEM_DISK: public

  MAIL_MAILER: ${MAIL_MAILER:-log}
  MAIL_HOST: ${MAIL_HOST:-}
  MAIL_PORT: ${MAIL_PORT:-2525}
  MAIL_USERNAME: ${MAIL_USERNAME:-}
  MAIL_PASSWORD: ${MAIL_PASSWORD:-}
  MAIL_FROM_ADDRESS: ${MAIL_FROM_ADDRESS:-hello@example.com}
  MAIL_FROM_NAME: ${APP_NAME:-Meet Meals}

services:
  app:
    build: .
    image: meet-meals-backend:latest
    restart: unless-stopped
    ports:
      - "${APP_PORT:-8080}:8080"
    environment:
      <<: *app-env
      # Cuma container web yang jalankan migrate / storage:link / config-cache
      # saat boot, supaya queue + scheduler tidak balapan menjalankannya juga.
      AUTORUN_ENABLED: "true"
    volumes:
      - storage:/var/www/html/storage
    depends_on:
      db:
        condition: service_healthy
    networks:
      - internal

  queue:
    image: meet-meals-backend:latest
    restart: unless-stopped
    command: ["php", "/var/www/html/artisan", "queue:work", "--tries=3", "--sleep=3", "--max-time=3600"]
    stop_signal: SIGTERM
    healthcheck:
      test: ["CMD", "healthcheck-queue"]
      start_period: 10s
    environment:
      <<: *app-env
      AUTORUN_ENABLED: "false"
    volumes:
      - storage:/var/www/html/storage
    depends_on:
      db:
        condition: service_healthy
      app:
        condition: service_started
    networks:
      - internal

  scheduler:
    image: meet-meals-backend:latest
    restart: unless-stopped
    command: ["php", "/var/www/html/artisan", "schedule:work"]
    stop_signal: SIGTERM
    healthcheck:
      test: ["CMD", "healthcheck-schedule"]
      start_period: 10s
    environment:
      <<: *app-env
      AUTORUN_ENABLED: "false"
    volumes:
      - storage:/var/www/html/storage
    depends_on:
      db:
        condition: service_healthy
      app:
        condition: service_started
    networks:
      - internal

  db:
    image: mysql:8.0
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: ${DB_DATABASE:-meet_meals}
      MYSQL_USER: ${DB_USERNAME:-meet_meals}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
    volumes:
      - db_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "${DB_USERNAME:-meet_meals}", "-p${DB_PASSWORD}"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s
    networks:
      - internal

volumes:
  db_data:
  storage:

networks:
  internal:
