# 瑞小美团队开发环境 Docker Compose 配置
# 域名：kpl.ireborn.com.cn
# 支持热重载，与演示系统完全隔离
#
# ⚠️ 敏感配置（API Key 等）请在 .env.kpl 文件中设置
# 启动命令：docker compose --env-file .env.kpl -f docker-compose.kpl.yml up -d

name: kpl-dev

services:
  # 前端开发服务
  kpl-frontend-dev:
    build:
      context: ./kaopeilian-frontend
      dockerfile: Dockerfile.dev
    container_name: kpl-frontend-dev
    restart: unless-stopped
    ports:
      - "3002:3001"
    environment:
      - NODE_ENV=development
      - VITE_APP_ENV=development
      - VITE_API_BASE_URL=https://kpl.ireborn.com.cn
      - VITE_WS_BASE_URL=wss://kpl.ireborn.com.cn
      - VITE_USE_MOCK_DATA=false
      - VITE_ENABLE_DEVTOOLS=true
      - VITE_ENABLE_REQUEST_LOG=true
      - CHOKIDAR_USEPOLLING=true  # 支持Docker中的文件监听
      - WATCHPACK_POLLING=true    # 支持热重载
    volumes:
      # 挂载源代码实现热重载（关键配置）
      - ./kaopeilian-frontend:/app
      - kpl_frontend_node_modules:/app/node_modules  # 使用命名卷
      - ./kaopeilian-frontend/logs:/app/logs
    networks:
      - kpl-dev-network
    depends_on:
      kpl-backend-dev:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3001/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # 后端开发服务
  kpl-backend-dev:
    build:
      context: ./kaopeilian-backend
      dockerfile: Dockerfile.dev
    container_name: kpl-backend-dev
    restart: unless-stopped
    ports:
      - "8001:8000"
    environment:
      - PYTHONPATH=/app
      - DEBUG=true
      - ENV=development
      - "DATABASE_URL=mysql+aiomysql://root:nj861021@kpl-mysql-dev:3306/kaopeilian?charset=utf8mb4"
      - REDIS_URL=redis://kpl-redis-dev:6379/0
      - MYSQL_HOST=kpl-mysql-dev
      - MYSQL_PORT=3306
      - MYSQL_USER=root
      - "MYSQL_PASSWORD=nj861021"
      - MYSQL_DATABASE=kaopeilian
      - REDIS_HOST=kpl-redis-dev
      - REDIS_PORT=6379
      - REDIS_DB=0
      - PYTHONUNBUFFERED=1  # 确保日志实时输出
      - CORS_ORIGINS=["https://kpl.ireborn.com.cn","http://localhost:3002","http://127.0.0.1:3002"]
      # Coze OAuth配置
      - COZE_OAUTH_CLIENT_ID=1114009328887
      - COZE_OAUTH_PUBLIC_KEY_ID=GGs9pw0BDHx2k9vGGehUyRgKV-PyUWLBncDs-YNNN_I
      - COZE_OAUTH_PRIVATE_KEY_PATH=/app/secrets/coze_private_key.pem
      - COZE_PRACTICE_BOT_ID=7560643598174683145
      # Coze 播课工作流配置（瑞小美专用）
      - COZE_BROADCAST_WORKFLOW_ID=7561161554420482088
      - COZE_BROADCAST_SPACE_ID=7474971491470688296
      - COZE_BROADCAST_BOT_ID=7560643598174683145
      # 完全禁用代理（覆盖 Docker Desktop 的代理配置）
      - HTTP_PROXY=
      - HTTPS_PROXY=
      - http_proxy=
      - https_proxy=
      - NO_PROXY=
      - no_proxy=
      # AI 服务配置（遵循瑞小美 AI 接入规范）
      # 注意：API Key 应从 .env 文件或密钥管理服务获取
      - AI_PRIMARY_API_KEY=${AI_PRIMARY_API_KEY:-}
      - AI_ANTHROPIC_API_KEY=${AI_ANTHROPIC_API_KEY:-}
      - AI_PRIMARY_BASE_URL=https://4sapi.com/v1
      - AI_FALLBACK_API_KEY=${AI_FALLBACK_API_KEY:-}
      - AI_FALLBACK_BASE_URL=https://openrouter.ai/api/v1
      # 默认模型：遵循"优先最强"原则，使用 Claude Opus 4.5
      - AI_DEFAULT_MODEL=claude-opus-4-5-20251101-thinking
      - AI_TIMEOUT=120
    volumes:
      # 挂载源代码实现热重载（关键配置）
      - ./kaopeilian-backend:/app:rw
      - /data/kaopeilian/uploads/kpl:/app/uploads:rw  # 迁移到数据盘
      - ./kaopeilian-backend/logs:/app/logs:rw
      - ./kaopeilian-backend/secrets:/app/secrets:ro
      # 排除虚拟环境目录，避免冲突
      - /app/venv
    networks:
      - kpl-dev-network
    depends_on:
      kpl-mysql-dev:
        condition: service_healthy
      kpl-redis-dev:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # 开发数据库
  kpl-mysql-dev:
    image: mysql:8.0.43
    container_name: kpl-mysql-dev
    restart: unless-stopped
    ports:
      - "3308:3306"
    environment:
      MYSQL_ROOT_PASSWORD: nj861021
      MYSQL_DATABASE: kaopeilian
      MYSQL_CHARACTER_SET_SERVER: utf8mb4
      MYSQL_COLLATION_SERVER: utf8mb4_unicode_ci
      TZ: Asia/Shanghai
    volumes:
      - kpl_mysql_dev_data:/var/lib/mysql
      - ./kaopeilian-backend/scripts/init_database_unified.sql:/docker-entrypoint-initdb.d/init.sql:ro
      - ./kaopeilian-backend/mysql.cnf:/etc/mysql/conf.d/mysql.cnf:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    command: --default-authentication-plugin=mysql_native_password
    networks:
      - kpl-dev-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-pnj861021"]
      timeout: 20s
      retries: 10
      start_period: 30s

  # 开发 Redis 缓存
  kpl-redis-dev:
    image: redis:7.2-alpine
    container_name: kpl-redis-dev
    restart: unless-stopped
    ports:
      - "6380:6379"
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - kpl_redis_dev_data:/data
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - kpl-dev-network
    command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

  # phpMyAdmin 数据库管理界面（可选）
  kpl-phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    container_name: kpl-phpmyadmin-dev
    restart: unless-stopped
    ports:
      - "8081:80"
    environment:
      PMA_HOST: kpl-mysql-dev
      PMA_PORT: 3306
      PMA_USER: root
      PMA_PASSWORD: nj861021
    networks:
      - kpl-dev-network
    depends_on:
      kpl-mysql-dev:
        condition: service_healthy
    profiles:
      - admin  # 使用 profile 控制是否启动

# 开发网络
networks:
  kpl-dev-network:
    external: true
    name: kpl-dev-network

# 开发数据卷
volumes:
  kpl_mysql_dev_data:
    driver: local
    name: kpl-mysql-dev-data
  kpl_redis_dev_data:
    driver: local
    name: kpl-redis-dev-data
  kpl_frontend_node_modules:
    driver: local
    name: kpl-frontend-node-modules

