# Discord風チャットアプリケーション - Docker Compose設定
# 開発・ステージング・本番環境対応の複合設定
# PostgreSQL、Redis（将来拡張）、アプリケーションサーバーの統合構成

version: '3.8'

services:
  # PostgreSQLデータベースサービス
  database:
    image: postgres:16-alpine
    container_name: chat-app-db
    environment:
      POSTGRES_DB: ${DB_NAME:-chat_app}
      POSTGRES_USER: ${DB_USER:-postgres}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-postgres}
      POSTGRES_INITDB_ARGS: "--auth-host=scram-sha-256"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./schema.sql:/docker-entrypoint-initdb.d/01-schema.sql:ro
    ports:
      - "${DB_PORT:-5432}:5432"
    networks:
      - chat-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-postgres} -d ${DB_NAME:-chat_app}"]
      interval: 10s
      timeout: 5s
      retries: 5
    security_opt:
      - no-new-privileges:true
    deploy:
      resources:
        limits:
          cpus: '1.0'
          memory: 512M
        reservations:
          cpus: '0.25'
          memory: 256M

  # Redis - 将来のセッションストア・キャッシュ用（オプション）
  redis:
    image: redis:7-alpine
    container_name: chat-app-redis
    ports:
      - "${REDIS_PORT:-6379}:6379"
    volumes:
      - redis_data:/data
    networks:
      - chat-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
    security_opt:
      - no-new-privileges:true
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 128M
        reservations:
          cpus: '0.1'
          memory: 64M
    profiles:
      - full  # 完全構成時のみ起動

  # メインアプリケーションサービス
  app:
    build:
      context: .
      dockerfile: Dockerfile
      target: production
    container_name: chat-app
    environment:
      NODE_ENV: ${NODE_ENV:-production}
      PORT: ${PORT:-3000}
      
      # Database configuration
      DB_HOST: database
      DB_PORT: 5432
      DB_NAME: ${DB_NAME:-chat_app}
      DB_USER: ${DB_USER:-postgres}
      DB_PASSWORD: ${DB_PASSWORD:-postgres}
      
      # Connection pool configuration
      DB_POOL_MAX: ${DB_POOL_MAX:-40}
      DB_POOL_MIN: ${DB_POOL_MIN:-10}
      
      # Session configuration
      SESSION_SECRET: ${SESSION_SECRET}
      SESSION_MAX_AGE: ${SESSION_MAX_AGE:-86400000}
      
      # CORS configuration
      FRONTEND_URL: ${FRONTEND_URL:-http://localhost:3000}
      
      # Security settings
      HELMET_ENABLED: ${HELMET_ENABLED:-true}
      RATE_LIMIT_ENABLED: ${RATE_LIMIT_ENABLED:-true}
      
      # Monitoring (optional)
      LOG_LEVEL: ${LOG_LEVEL:-info}
      METRICS_ENABLED: ${METRICS_ENABLED:-false}
    
    ports:
      - "${PORT:-3000}:3000"
    volumes:
      # Bind mount for logs (optional)
      - ./logs:/app/logs:rw
    networks:
      - chat-network
    depends_on:
      database:
        condition: service_healthy
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    security_opt:
      - no-new-privileges:true
    deploy:
      resources:
        limits:
          cpus: '2.0'
          memory: 1G
        reservations:
          cpus: '0.5'
          memory: 256M
    user: "1001:1001"  # nodeapp user

  # Nginx リバースプロキシ（本番環境推奨）
  nginx:
    image: nginx:1.25-alpine
    container_name: chat-app-nginx
    ports:
      - "${NGINX_HTTP_PORT:-80}:80"
      - "${NGINX_HTTPS_PORT:-443}:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - ./public:/usr/share/nginx/html:ro
      # SSL certificates (if using HTTPS)
      - ./ssl:/etc/ssl/certs:ro
    networks:
      - chat-network
    depends_on:
      - app
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    security_opt:
      - no-new-privileges:true
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 128M
        reservations:
          cpus: '0.1'
          memory: 64M
    profiles:
      - production  # 本番環境でのみ使用

# 永続化ボリューム
volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local

# ネットワーク設定
networks:
  chat-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16