services:
  app:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    env_file:
      - .env
    environment:
      APP_ENV: production
      APP_DEBUG: "false"
      QUEUE_CONNECTION: redis
      CACHE_STORE: redis
      SESSION_DRIVER: redis
      REDIS_HOST: redis
      DB_HOST: mysql
    volumes:
      - storage:/var/www/html/storage
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_started
    restart: unless-stopped

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

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

  redis:
    image: redis:7-alpine
    command: redis-server --appendonly yes
    volumes:
      - redis_data:/data
    restart: unless-stopped

  queue:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    command: php artisan queue:work redis --queue=default,long --sleep=3 --tries=3 --max-time=3600 --backoff=10,30,60
    env_file:
      - .env
    environment:
      APP_ENV: production
      QUEUE_CONNECTION: redis
      REDIS_HOST: redis
      DB_HOST: mysql
    volumes:
      - storage:/var/www/html/storage
    depends_on:
      - app
      - redis
    restart: unless-stopped

  scheduler:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    command: php artisan schedule:work
    env_file:
      - .env
    environment:
      APP_ENV: production
      REDIS_HOST: redis
      DB_HOST: mysql
    volumes:
      - storage:/var/www/html/storage
    depends_on:
      - app
    restart: unless-stopped

  reverb:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    command: php artisan reverb:start --host=0.0.0.0 --port=8080
    env_file:
      - .env
    environment:
      APP_ENV: production
      BROADCAST_CONNECTION: reverb
      REDIS_HOST: redis
    ports:
      - "${REVERB_PORT:-8081}:8080"
    volumes:
      - storage:/var/www/html/storage
    depends_on:
      - redis
    restart: unless-stopped

volumes:
  mysql_data:
  redis_data:
  storage:
