# WordPress Stack
# WordPress + MySQL + Redis Cache + phpMyAdmin
# Production-ready WordPress deployment

services:
  # WordPress Application
  wordpress:
    image: wordpress:6.4-php8.2-fpm-alpine
    container_name: wordpress
    restart: unless-stopped
    env_file:
      - .env
    environment:
      - WORDPRESS_DB_HOST=db:3306
      - WORDPRESS_DB_USER=${MYSQL_USER}
      - WORDPRESS_DB_PASSWORD=${MYSQL_PASSWORD}
      - WORDPRESS_DB_NAME=${MYSQL_DATABASE}
      - WORDPRESS_TABLE_PREFIX=${WP_TABLE_PREFIX:-wp_}
      - WORDPRESS_CONFIG_EXTRA=
          define('WP_REDIS_HOST', 'redis');
          define('WP_REDIS_PORT', 6379);
          define('WP_REDIS_PASSWORD', '${REDIS_PASSWORD}');
          define('WP_CACHE', true);
          define('WP_MEMORY_LIMIT', '256M');
    volumes:
      - wordpress-data:/var/www/html
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini:ro
    networks:
      - frontend
      - backend
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "php-fpm-healthcheck"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    deploy:
      resources:
        limits:
          cpus: '1.0'
          memory: 512M
        reservations:
          cpus: '0.5'
          memory: 256M

  # Nginx Web Server
  nginx:
    image: nginx:1.25-alpine
    container_name: wordpress-nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - wordpress-data:/var/www/html:ro
      - ./nginx/wordpress.conf:/etc/nginx/conf.d/default.conf:ro
      - ./nginx/ssl:/etc/nginx/ssl:ro
    networks:
      - frontend
    depends_on:
      - wordpress
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost/nginx-health"]
      interval: 30s
      timeout: 10s
      retries: 3
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 256M

  # MySQL Database
  db:
    image: mysql:8.2
    container_name: wordpress-db
    restart: unless-stopped
    env_file:
      - .env
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
    volumes:
      - mysql-data:/var/lib/mysql
      - ./mysql/my.cnf:/etc/mysql/conf.d/custom.cnf:ro
    networks:
      - backend
    command:
      - --default-authentication-plugin=mysql_native_password
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
      - --max_allowed_packet=256M
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    deploy:
      resources:
        limits:
          cpus: '2.0'
          memory: 2G
        reservations:
          cpus: '1.0'
          memory: 1G
    security_opt:
      - no-new-privileges:true

  # Redis Cache
  redis:
    image: redis:7.2-alpine
    container_name: wordpress-redis
    restart: unless-stopped
    command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD} --maxmemory 256mb --maxmemory-policy allkeys-lru
    env_file:
      - .env
    volumes:
      - redis-data:/data
    networks:
      - backend
    healthcheck:
      test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 256M
    security_opt:
      - no-new-privileges:true

  # phpMyAdmin (Optional - for database management)
  phpmyadmin:
    image: phpmyadmin:5.2-fpm-alpine
    container_name: wordpress-phpmyadmin
    restart: unless-stopped
    env_file:
      - .env
    environment:
      - PMA_HOST=db
      - PMA_PORT=3306
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - UPLOAD_LIMIT=100M
    ports:
      - "8080:80"
    networks:
      - backend
    depends_on:
      db:
        condition: service_healthy
    profiles:
      - admin
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 512M

  # WordPress CLI (for management tasks)
  wp-cli:
    image: wordpress:cli-php8.2
    container_name: wordpress-cli
    user: "33:33"  # www-data user
    env_file:
      - .env
    environment:
      - WORDPRESS_DB_HOST=db:3306
      - WORDPRESS_DB_USER=${MYSQL_USER}
      - WORDPRESS_DB_PASSWORD=${MYSQL_PASSWORD}
      - WORDPRESS_DB_NAME=${MYSQL_DATABASE}
    volumes:
      - wordpress-data:/var/www/html
    networks:
      - backend
    depends_on:
      db:
        condition: service_healthy
    profiles:
      - cli
    entrypoint: wp
    command: --info

networks:
  frontend:
    driver: bridge
  backend:
    driver: bridge
    internal: true

volumes:
  wordpress-data:
    driver: local
  mysql-data:
    driver: local
  redis-data:
    driver: local
