services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: beres-commerce:latest
    container_name: beres-app
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_started
    env_file: .env
    environment:
      DB_HOST: mysql
      DB_PORT: 3306
    volumes:
      - ./.env:/var/www/html/.env
      - ./storage:/var/www/html/storage
      - app_public:/var/www/html/public
    networks:
      - beres

  nginx:
    image: nginx:1.27-alpine
    container_name: beres-nginx
    restart: unless-stopped
    depends_on:
      - app
    ports:
      - "${APP_PORT:-280}:80"
    volumes:
      - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf:ro
      - app_public:/var/www/html/public:ro
      - ./storage:/var/www/html/storage:ro
    networks:
      - beres

  mysql:
    image: mysql:8.0
    container_name: beres-mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_USER: ${DB_USERNAME}
      MYSQL_PASSWORD: ${DB_PASSWORD}
    ports:
      - "${DB_PORT_HOST:-3506}:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${DB_ROOT_PASSWORD}"]
      interval: 10s
      timeout: 5s
      retries: 10
    networks:
      - beres

  redis:
    image: redis:7-alpine
    container_name: beres-redis
    restart: unless-stopped
    ports:
      - "${REDIS_PORT_HOST:-6579}:6379"
    volumes:
      - redis_data:/data
    networks:
      - beres

  queue:
    image: beres-commerce:latest
    container_name: beres-queue
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_started
    env_file: .env
    environment:
      DB_HOST: mysql
      DB_PORT: 3306
    command: php artisan queue:work --sleep=3 --tries=3 --max-time=3600
    volumes:
      - ./.env:/var/www/html/.env
      - ./storage:/var/www/html/storage
    networks:
      - beres

  scheduler:
    image: beres-commerce:latest
    container_name: beres-scheduler
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
    env_file: .env
    environment:
      DB_HOST: mysql
      DB_PORT: 3306
    command: >
      sh -c "while true; do php artisan schedule:run --verbose --no-interaction >> /dev/null 2>&1; sleep 60; done"
    volumes:
      - ./.env:/var/www/html/.env
      - ./storage:/var/www/html/storage
    networks:
      - beres

volumes:
  mysql_data:
  redis_data:
  app_public:

networks:
  beres:
    driver: bridge
