version: "3.9"

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        PHP_VERSION: 8.3
        NODE_VERSION: 20
    image: fotospiel-app:latest
    env_file:
      - docker/.env.docker
    environment:
      APP_ENV: ${APP_ENV:-production}
      APP_DEBUG: ${APP_DEBUG:-false}
      APP_SOURCE: /opt/app
      APP_TARGET: /var/www/html
      APP_USER: www-data
      APP_GROUP: www-data
    volumes:
      - app-code:/var/www/html
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_started
    restart: unless-stopped

  web:
    image: nginx:1.25-alpine
    depends_on:
      - app
    volumes:
      - app-code:/var/www/html:ro
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      - ./docs/site/build:/var/www/docs-site:ro
      - ./docker/nginx/.htpasswd-docs:/etc/nginx/.htpasswd-docs:ro
    ports:
      - "${APP_HTTP_PORT:-8080}:80"
    restart: unless-stopped

  queue:
    image: fotospiel-app:latest
    command: /var/www/html/scripts/queue-worker.sh default
    env_file:
      - docker/.env.docker
    environment:
      APP_ENV: ${APP_ENV:-production}
    volumes:
      - app-code:/var/www/html
    depends_on:
      - app
      - redis
    restart: unless-stopped

  media-storage-worker:
    image: fotospiel-app:latest
    command: /var/www/html/scripts/queue-worker.sh media-storage
    env_file:
      - docker/.env.docker
    environment:
      APP_ENV: ${APP_ENV:-production}
      QUEUE_TRIES: 5
      QUEUE_SLEEP: 5
    volumes:
      - app-code:/var/www/html
    depends_on:
      - app
      - redis
    restart: unless-stopped

  scheduler:
    image: fotospiel-app:latest
    command: php artisan schedule:work
    env_file:
      - docker/.env.docker
    environment:
      APP_ENV: ${APP_ENV:-production}
    volumes:
      - app-code:/var/www/html
    depends_on:
      - app
    restart: unless-stopped

  horizon:
    image: fotospiel-app:latest
    command: /var/www/html/scripts/horizon.sh
    env_file:
      - docker/.env.docker
    environment:
      APP_ENV: ${APP_ENV:-production}
    volumes:
      - app-code:/var/www/html
    depends_on:
      - app
      - redis
    restart: unless-stopped
    profiles: ["horizon"]

  redis:
    image: redis:7.4-alpine
    command: ["redis-server", "--save", "", "--appendonly", "no"]
    volumes:
      - redis-data:/data
    ports:
      - "${APP_REDIS_PORT:-6379}:6379"
    restart: unless-stopped

  mysql:
    image: mysql:8.4
    environment:
      MYSQL_DATABASE: ${DB_DATABASE:-fotospiel}
      MYSQL_USER: ${DB_USERNAME:-fotospiel}
      MYSQL_PASSWORD: ${DB_PASSWORD:-secret}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-secret-root}
    volumes:
      - mysql-data:/var/lib/mysql
    ports:
      - "${APP_DB_PORT:-3306}:3306"
    healthcheck:
      test:
        [
          "CMD",
          "mysqladmin",
          "ping",
          "-h",
          "127.0.0.1",
          "-p${DB_PASSWORD:-secret}"
        ]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped

volumes:
  app-code:
  mysql-data:
  redis-data:
