# 考培练系统开发环境 Docker Compose 配置
name: kaopeilian-dev

services:
  # 前端开发服务
  frontend-dev:
    build:
      context: ./kaopeilian-frontend
      dockerfile: Dockerfile.dev
    container_name: kaopeilian-frontend-dev
    restart: unless-stopped
    ports:
      - "3001:3001"
    environment:
      - NODE_ENV=development
      - VITE_APP_ENV=development
      - VITE_API_BASE_URL=""
      - VITE_WS_BASE_URL=""
      - 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
      - kaopeilian_frontend_node_modules:/app/node_modules  # 使用命名卷
      - ./kaopeilian-frontend/logs:/app/logs
    networks:
      - kaopeilian-dev-network
    depends_on:
      backend-dev:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3001/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # 后端开发服务
  backend-dev:
    build:
      context: ./kaopeilian-backend
      dockerfile: Dockerfile.dev
    container_name: kaopeilian-backend-dev
    restart: unless-stopped
    ports:
      - "8000:8000"
    environment:
      - PYTHONPATH=/app
      - DEBUG=true
      - ENV=development
      - "DATABASE_URL=mysql+aiomysql://root:nj861021@mysql-dev:3306/kaopeilian?charset=utf8mb4"
      - REDIS_URL=redis://redis-dev:6379/0
      - MYSQL_HOST=mysql-dev
      - MYSQL_PORT=3306
      - MYSQL_USER=root
      - "MYSQL_PASSWORD=nj861021"
      - MYSQL_DATABASE=kaopeilian
      - REDIS_HOST=redis-dev
      - REDIS_PORT=6379
      - REDIS_DB=0
      - PYTHONUNBUFFERED=1  # 确保日志实时输出
      - CORS_ORIGINS=["http://localhost:3000","http://localhost:3001","http://localhost:5173","http://127.0.0.1:3000","http://127.0.0.1:3001","http://127.0.0.1:5173"]
      # 完全禁用代理（覆盖 Docker Desktop 的代理配置）
      - HTTP_PROXY=
      - HTTPS_PROXY=
      - http_proxy=
      - https_proxy=
      - NO_PROXY=
      - no_proxy=
    volumes:
      # 挂载源代码实现热重载（关键配置）
      - ./kaopeilian-backend:/app:rw
      - ./kaopeilian-backend/uploads:/app/uploads:rw
      - ./kaopeilian-backend/logs:/app/logs:rw
      # 排除虚拟环境目录，避免冲突
      - /app/venv
    networks:
      - kaopeilian-dev-network
    depends_on:
      mysql-dev:
        condition: service_healthy
      redis-dev:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # 开发数据库
  mysql-dev:
    image: mysql:8.0.43
    container_name: kaopeilian-mysql-dev
    restart: unless-stopped
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: nj861021
      MYSQL_DATABASE: kaopeilian
      MYSQL_CHARACTER_SET_SERVER: utf8mb4
      MYSQL_COLLATION_SERVER: utf8mb4_unicode_ci
    volumes:
      - 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
    command: --default-authentication-plugin=mysql_native_password
    networks:
      - kaopeilian-dev-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-pnj861021"]
      timeout: 20s
      retries: 10
      start_period: 30s

  # 开发 Redis 缓存
  redis-dev:
    image: redis:7.2-alpine
    container_name: kaopeilian-redis-dev
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - redis_dev_data:/data
    networks:
      - kaopeilian-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 数据库管理界面（可选）
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    container_name: kaopeilian-phpmyadmin-dev
    restart: unless-stopped
    ports:
      - "8080:80"
    environment:
      PMA_HOST: mysql-dev
      PMA_PORT: 3306
      PMA_USER: root
      PMA_PASSWORD: nj861021
    networks:
      - kaopeilian-dev-network
    depends_on:
      mysql-dev:
        condition: service_healthy
    profiles:
      - admin  # 使用 profile 控制是否启动

  # 邮件测试服务（开发环境用于测试邮件发送）
  mailhog:
    image: mailhog/mailhog:latest
    container_name: kaopeilian-mailhog-dev
    restart: unless-stopped
    ports:
      - "1025:1025"  # SMTP 端口
      - "8025:8025"  # Web UI 端口
    networks:
      - kaopeilian-dev-network
    profiles:
      - mail  # 使用 profile 控制是否启动

# 开发网络
networks:
  kaopeilian-dev-network:
    driver: bridge
    name: kaopeilian-dev-network

# 开发数据卷
volumes:
  mysql_dev_data:
    driver: local
    name: kaopeilian-mysql-dev-data
  redis_dev_data:
    driver: local
    name: kaopeilian-redis-dev-data
  kaopeilian_frontend_node_modules:
    driver: local
    name: kaopeilian-frontend-node-modules
