version: '3.8'

services:
  app:
    build:
      context: ..
      dockerfile: infra/Dockerfile
    container_name: laravel_app
    restart: unless-stopped
    working_dir: /var/www/html
    networks:
      - laravel_network
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    env_file:
      - ../app/.env
    mem_limit: 512m
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"
    healthcheck:
      test: ["CMD-SHELL", "php artisan about > /dev/null 2>&1"]
      interval: 10s
      timeout: 3s
      retries: 3

  queue:
    build:
      context: ..
      dockerfile: infra/Dockerfile
    container_name: laravel_queue
    restart: unless-stopped
    working_dir: /var/www/html
    command: php artisan queue:work --tries=3 --backoff=5 --timeout=90 --sleep=3 --max-time=3600 --verbose
    env_file:
      - ../app/.env
    networks:
      - laravel_network
    depends_on:
      redis:
        condition: service_healthy
    mem_limit: 512m
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"

  scheduler:
    build:
      context: ..
      dockerfile: infra/Dockerfile
    container_name: laravel_scheduler
    restart: unless-stopped
    working_dir: /var/www/html
    command: php artisan schedule:work
    env_file:
      - ../app/.env
    networks:
      - laravel_network
    depends_on:
      app:
        condition: service_started
    mem_limit: 128m
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"

  nginx:
    image: nginx:1.27-alpine
    container_name: laravel_nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ../app:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      - ./nginx/certs:/etc/nginx/certs:ro
    networks:
      - laravel_network
    depends_on:
      app:
        condition: service_healthy
    mem_limit: 128m
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"

  mysql:
    image: mysql:8.0
    container_name: laravel_mysql
    restart: unless-stopped
    command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    env_file:
      - ../app/.env
    volumes:
      - mysql_data:/var/lib/mysql
    networks:
      - laravel_network
    mem_limit: 512m
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h localhost -uroot -p\"$$MYSQL_ROOT_PASSWORD\" || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s

  redis:
    image: redis:7-alpine
    container_name: laravel_redis
    restart: unless-stopped
    env_file:
      - ../app/.env
    command: >
      sh -c "exec redis-server --requirepass \"$$REDIS_PASSWORD\" --appendonly yes"
    networks:
      - laravel_network
    volumes:
      - redis_data:/data
    mem_limit: 128m
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"
    healthcheck:
      test: ["CMD-SHELL", "redis-cli -a \"$$REDIS_PASSWORD\" ping || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 3

networks:
  laravel_network:
    driver: bridge

volumes:
  mysql_data:
  redis_data:
