version: "3.9"

networks:
  backend:

volumes:
  mysql_data:
  redis_data:
  storage_data:
  bootstrap_cache:

services:
  app:
    build:
      context: .
      target: php_app
    image: stylegallery-app:latest
    env_file:
      - .env.docker
    restart: unless-stopped
    command: >
      sh -c "
      mkdir -p storage/app/public storage/logs \
      storage/framework/cache storage/framework/data storage/framework/sessions \
      storage/framework/testing storage/framework/views \
      bootstrap/cache &&
      chown -R www-data:www-data storage storage/app storage/framework storage/logs bootstrap/cache &&
      php artisan package:discover --ansi &&
      php artisan storage:link --ansi || true &&
      php artisan config:cache --ansi &&
      php artisan route:cache --ansi &&
      php artisan view:cache --ansi &&
      php-fpm
      "
    volumes:
      - storage_data:/var/www/html/storage
      - bootstrap_cache:/var/www/html/bootstrap/cache
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - backend
    healthcheck:
      test: ["CMD", "php", "-v"]
      interval: 30s
      timeout: 5s
      retries: 3

  queue:
    image: stylegallery-app:latest
    env_file:
      - .env.docker
    restart: unless-stopped
    command: >
      sh -c "
      mkdir -p storage/app/public storage/logs \
      storage/framework/cache storage/framework/data storage/framework/sessions \
      storage/framework/testing storage/framework/views \
      bootstrap/cache &&
      chown -R www-data:www-data storage storage/app storage/framework storage/logs bootstrap/cache &&
      php artisan queue:work --sleep=3 --tries=3 --max-time=3600
      "
    volumes:
      - storage_data:/var/www/html/storage
      - bootstrap_cache:/var/www/html/bootstrap/cache
    depends_on:
      app:
        condition: service_started
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - backend

  scheduler:
    image: stylegallery-app:latest
    env_file:
      - .env.docker
    restart: unless-stopped
    command: >
      sh -c "
      mkdir -p storage/app/public storage/logs \
      storage/framework/cache storage/framework/data storage/framework/sessions \
      storage/framework/testing storage/framework/views \
      bootstrap/cache &&
      chown -R www-data:www-data storage storage/app storage/framework storage/logs bootstrap/cache &&
      php artisan schedule:work
      "
    volumes:
      - storage_data:/var/www/html/storage
      - bootstrap_cache:/var/www/html/bootstrap/cache
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - backend

  nginx:
    build:
      context: .
      target: nginx
    image: stylegallery-nginx:latest
    restart: unless-stopped
    depends_on:
      app:
        condition: service_started
    networks:
      - backend
    volumes:
      - storage_data:/var/www/html/storage:ro
      - bootstrap_cache:/var/www/html/bootstrap/cache:ro

  mysql:
    image: mysql:8.0
    env_file:
      - .env.docker
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_USER: ${DB_USERNAME}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
    command: ["mysqld", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"]
    volumes:
      - mysql_data:/var/lib/mysql
    networks:
      - backend
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h localhost -p${DB_PASSWORD}"]
      interval: 30s
      timeout: 10s
      retries: 5

  redis:
    image: redis:7-alpine
    env_file:
      - .env.docker
    restart: unless-stopped
    command: ["sh", "-c", "exec redis-server --appendonly yes --requirepass \"$REDIS_PASSWORD\""]
    volumes:
      - redis_data:/data
    networks:
      - backend
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD}", "ping"]
      interval: 30s
      timeout: 5s
      retries: 3
