services:
  # PostgreSQL 数据库服务
  qaup-postgres:
    image: postgis/postgis:17-3.5-alpine
    container_name: qaup-postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-qaup}
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-qaup123}
      APP_DB_USER: ${APP_DB_USER:-qaup_app}
      APP_DB_PASSWORD: ${APP_DB_PASSWORD:-qaup123}
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ../docker/postgres/init-db.sh:/docker-entrypoint-initdb.d/01-init-db.sh:ro
      - ../docker/postgres/qaup_database_schema.sql:/docker-entrypoint-initdb.d/02-schema.sql:ro
      - ../docker/postgres/postgresql.conf:/etc/postgresql/postgresql.conf:ro
      - postgres_backup:/backup
      - postgres_logs:/var/log/postgresql
    ports:
      - "${POSTGRES_PORT:-5432}:5432"
    networks:
      - qaup-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -h localhost -p 5432 -U postgres"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s
    deploy:
      resources:
        limits:
          memory: 1G
          cpus: '1.0'
        reservations:
          memory: 512M
          cpus: '0.5'

  # Redis 缓存服务
  qaup-redis:
    image: redis:8.0-alpine
    container_name: qaup-redis
    restart: unless-stopped
    command: ["redis-server", "--appendonly", "yes", "--maxmemory", "256mb", "--maxmemory-policy", "allkeys-lru"]
    volumes:
      - redis_data:/data
      - redis_logs:/var/log/redis
    ports:
      - "${REDIS_PORT:-6379}:6379"
    networks:
      - qaup-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    deploy:
      resources:
        limits:
          memory: 512M
          cpus: '0.5'
        reservations:
          memory: 128M
          cpus: '0.1'

  # QAUP 应用服务
  qaup-app:
    image: qaup-app:latest
    container_name: qaup-app
    restart: unless-stopped
    environment:
      # Spring 配置
      SPRING_PROFILES_ACTIVE: ${SPRING_PROFILES_ACTIVE:-prod}
      
      # 数据库配置
      DB_HOST: qaup-postgres
      DB_PORT: 5432
      DB_NAME: ${POSTGRES_DB:-qaup}
      DB_USERNAME: ${APP_DB_USER:-qaup_app}
      DB_PASSWORD: ${APP_DB_PASSWORD:-qaup123}
      
      # Redis 配置
      REDIS_HOST: qaup-redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      
      # 应用配置
      SERVER_PORT: 8080
      UPLOAD_PATH: /app/uploadPath
      LOG_PATH: /app/logs
      
      # JVM 配置
      JVM_OPTS: ${JVM_OPTS:--Dname=qaup-admin.jar -Duser.timezone=Asia/Shanghai -Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m}
    volumes:
      - app_logs:/app/logs
      - app_uploads:/app/uploadPath
      - app_temp:/tmp
      - ./config:/app/config
    ports:
      - "${APP_PORT:-8080}:8080"
    networks:
      - qaup-network
    depends_on:
      qaup-postgres:
        condition: service_healthy
      qaup-redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 120s
    deploy:
      resources:
        limits:
          memory: 2G
          cpus: '2.0'
        reservations:
          memory: 1G
          cpus: '1.0'

  # Nginx 反向代理服务
  qaup-nginx:
    image: qaup-nginx:latest
    container_name: qaup-nginx
    restart: unless-stopped
    environment:
      NGINX_PORT: 80
      SERVER_NAME: ${SERVER_NAME:-localhost}
      API_PREFIX: ${API_PREFIX:-/prod-api}
      BACKEND_HOST: qaup-app
      BACKEND_PORT: 8080
      MAX_UPLOAD_SIZE: ${MAX_UPLOAD_SIZE:-20M}
    volumes:
      - nginx_logs:/var/log/nginx
      - nginx_cache:/var/cache/nginx
    ports:
      - "${NGINX_PORT:-80}:80"
      - "${NGINX_HTTPS_PORT:-443}:443"
    networks:
      - qaup-network
    depends_on:
      qaup-app:
        condition: service_healthy
    command: ["nginx", "-g", "daemon off;"]
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    deploy:
      resources:
        limits:
          memory: 256M
          cpus: '0.5'
        reservations:
          memory: 64M
          cpus: '0.1'

# 数据卷定义
volumes:
  postgres_data:
    driver: local
  postgres_backup:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${PWD}/backup/postgres
  postgres_logs:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${PWD}/logs/postgres
  redis_data:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${PWD}/data/redis
  redis_logs:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${PWD}/logs/redis
  app_logs:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${PWD}/logs/app
  app_uploads:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${PWD}/data/uploads
  app_temp:
    driver: local
  nginx_logs:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${PWD}/logs/nginx
  nginx_cache:
    driver: local

# 网络定义
networks:
  qaup-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.30.0.0/16
