services:
  app:
    build:
      context: .
      target: app
    restart: unless-stopped
    env_file: .env
    environment:
      RUN_MIGRATIONS: "true"
    volumes:
      - storage_data:/var/www/html/storage/app
      - static_build:/var/www/html/public/build
    depends_on:
      - mysql
      - redis
    healthcheck:
      test: ["CMD", "php", "artisan", "--version"]
      interval: 10s
      timeout: 5s
      retries: 6
      start_period: 30s

  nginx:
    image: nginx:alpine
    restart: unless-stopped
    ports:
      - "${APP_PORT:-80}:80"
    volumes:
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      - ./docker/nginx/snippets:/etc/nginx/snippets:ro
      - static_build:/var/www/html/public/build:ro
    depends_on:
      - app
    healthcheck:
      test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://localhost/up"]
      interval: 10s
      timeout: 5s
      retries: 6
      start_period: 30s

  horizon:
    build:
      context: .
      target: app
    restart: unless-stopped
    env_file: .env
    environment:
      RUN_MIGRATIONS: "false"
    command: php artisan horizon
    volumes:
      - storage_data:/var/www/html/storage/app
    depends_on:
      - mysql
      - redis

  scheduler:
    build:
      context: .
      target: app
    restart: unless-stopped
    env_file: .env
    environment:
      RUN_MIGRATIONS: "false"
    command: sh -c "while true; do php artisan schedule:run; sleep 60; done"
    volumes:
      - storage_data:/var/www/html/storage/app
    depends_on:
      - mysql
      - redis

  mysql:
    image: mysql:8.0
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_USER: ${DB_USERNAME}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
    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
      start_period: 30s

  redis:
    image: redis:alpine
    restart: unless-stopped
    volumes:
      - redis_data:/data

volumes:
  mysql_data:
  redis_data:
  storage_data:
  static_build:
