# Production Docker Compose Configuration
# Optimized for high concurrency (1000+ concurrent users)
services:
  # Nginx web server - handles HTTP requests and serves static files
  nginx:
    image: nginx:alpine
    container_name: cbt-nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./docker/nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - php-fpm
    networks:
      - cbt-network
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # PHP-FPM - handles PHP processing with optimized configuration
  php-fpm:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: cbt-php-fpm
    restart: unless-stopped
    volumes:
      - ./:/var/www
      - ./docker/php-fpm.conf:/usr/local/etc/php-fpm.d/zz-docker.conf:ro
    environment:
      APP_ENV: production
      APP_DEBUG: false
      DB_CONNECTION: pgsql
      DB_HOST: ${DB_HOST}
      DB_PORT: ${DB_PORT:-5432}
      DB_DATABASE: ${DB_DATABASE}
      DB_USERNAME: ${DB_USERNAME}
      DB_PASSWORD: ${DB_PASSWORD}
      DB_MAX_CONNECTIONS: 200
      DB_POOL_MAX_IDLE_TIME: 60
      REDIS_HOST: ${REDIS_HOST:-redis}
      REDIS_PORT: ${REDIS_PORT:-6379}
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_MAX_CONNECTIONS: 100
      REDIS_TIMEOUT: 5.0
      REDIS_READ_TIMEOUT: 5.0
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - cbt-network
    healthcheck:
      test: ["CMD-SHELL", "php-fpm-healthcheck || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # PostgreSQL 16 - Primary Database
  postgres:
    image: postgres:16-alpine
    container_name: cbt-postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${DB_DATABASE:-cbt}
      POSTGRES_USER: ${DB_USERNAME:-postgres}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-postgres}
      PGDATA: /var/lib/postgresql/data/pgdata
      # PostgreSQL performance optimizations
      POSTGRES_INITDB_ARGS: "-E UTF8 --locale=C"
    ports:
      - "${DB_PORT:-5432}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./docker/postgresql.conf:/etc/postgresql/postgresql.conf:ro
    command: postgres -c config_file=/etc/postgresql/postgresql.conf
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USERNAME:-postgres} -d ${DB_DATABASE:-cbt}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    networks:
      - cbt-network

  # Redis 7 - Caching and Sessions
  redis:
    image: redis:7-alpine
    container_name: cbt-redis
    restart: always
    ports:
      - "${REDIS_PORT:-6379}:6379"
    volumes:
      - redis_data:/data
    command: >
      sh -c '
      if [ -n "$$REDIS_PASSWORD" ]; then
        redis-server --requirepass $$REDIS_PASSWORD --maxclients 10000 --maxmemory 1gb --maxmemory-policy allkeys-lru --save 60 1000 --appendonly yes
      else
        redis-server --maxclients 10000 --maxmemory 1gb --maxmemory-policy allkeys-lru --save 60 1000 --appendonly yes
      fi
      '
    healthcheck:
      test: >
        sh -c '
        if [ -n "$$REDIS_PASSWORD" ]; then
          redis-cli -a $$REDIS_PASSWORD --no-auth-warning ping
        else
          redis-cli ping
        fi
        '
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 10s
    networks:
      - cbt-network
    deploy:
      resources:
        limits:
          memory: 1.5G
        reservations:
          memory: 512M

networks:
  cbt-network:
    driver: bridge

volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local
