services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
      target: application
    image: mtwu-app:prod
    container_name: mtwu-app
    restart: always
    env_file: .env.production
    volumes:
      - ./storage:/var/www/storage
    networks:
      - mtwu-prod-network
    depends_on:
      - db
      - redis

  nginx:
    image: nginx:alpine
    container_name: mtwu-nginx
    restart: always
    ports:
      - "${APP_PORT:-8000}:80"
    volumes:
      - ./public:/var/www/public
      - ./nginx/conf.d:/etc/nginx/conf.d
    networks:
      - mtwu-prod-network
    depends_on:
      - app

  db:
    image: mysql:8.0
    container_name: mtwu-db
    restart: always
    env_file: .env.production
    environment:
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_USER: ${DB_USERNAME}
    volumes:
      - dbdata:/var/lib/mysql
    networks:
      - mtwu-prod-network
    healthcheck:
      test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:alpine
    container_name: mtwu-redis
    restart: always
    command: redis-server --appendonly yes --requirepass "${REDIS_PASSWORD}"
    volumes:
      - redisdata:/data
    networks:
      - mtwu-prod-network

  worker:
    image: mtwu-app:prod
    container_name: mtwu-worker
    restart: always
    env_file: .env.production
    command: php artisan queue:work --tries=3 --timeout=90
    volumes:
      - ./storage:/var/www/storage
    networks:
      - mtwu-prod-network
    depends_on:
      - app
      - db
      - redis

  scheduler:
    image: mtwu-app:prod
    container_name: mtwu-scheduler
    restart: always
    env_file: .env.production
    command: php artisan schedule:work
    volumes:
      - ./storage:/var/www/storage
    networks:
      - mtwu-prod-network
    depends_on:
      - app

networks:
  mtwu-prod-network:
    driver: bridge

volumes:
  dbdata:
    driver: local
  redisdata:
    driver: local
