services:
  mysql:
    image: mysql:8.4
    container_name: ai_kb_v2_mysql
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: ai_knowledge_base_v2
      MYSQL_USER: ai_kb
      MYSQL_PASSWORD: ai_kb
      TZ: Asia/Shanghai
    command:
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
    ports:
      - "3307:3306"
    volumes:
      - ai_kb_v2_mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -uai_kb -pai_kb --silent"]
      interval: 5s
      timeout: 5s
      retries: 20

  redis:
    image: redis:7-alpine
    container_name: ai_kb_v2_redis
    ports:
      - "6380:6379"
    command: ["redis-server", "--appendonly", "yes"]
    volumes:
      - ai_kb_v2_redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 20

  backend:
    build:
      context: ./apps/backend
    container_name: ai_kb_v2_backend
    environment:
      APP_ENV: ${APP_ENV:-docker}
      DEBUG: "true"
      DATABASE_URL: mysql+pymysql://ai_kb:ai_kb@mysql:3306/ai_knowledge_base_v2?charset=utf8mb4
      REDIS_URL: redis://redis:6379/0
      BACKEND_WORKERS: "1"
      DB_POOL_SIZE: "10"
      DB_MAX_OVERFLOW: "20"
      DB_POOL_TIMEOUT: "30"
      DB_POOL_RECYCLE: "1800"
      JWT_SECRET_KEY: local-dev-secret-change-before-production
      MOCK_SMS_ENABLED: "true"
      MOCK_SMS_CODE: "123456"
      MOCK_RAG_ENABLED: "false"
      MOCK_MODEL_ENABLED: "true"
      FEISHU_MOCK_ENABLED: "false"
      FEISHU_SEARCH_URL: "${FEISHU_SEARCH_URL:-}"
      FEISHU_APP_ID: "${FEISHU_APP_ID:-}"
      FEISHU_APP_SECRET: "${FEISHU_APP_SECRET:-}"
      BOOTSTRAP_ADMIN_USERNAME: ${BOOTSTRAP_ADMIN_USERNAME:-admin}
      BOOTSTRAP_ADMIN_PASSWORD: ${BOOTSTRAP_ADMIN_PASSWORD:-admin123456}
      BOOTSTRAP_ADMIN_NAME: ${BOOTSTRAP_ADMIN_NAME:-系统管理员}
      AUTO_CREATE_TABLES: "false"
    ports:
      - "8100:8100"
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy

  user-client:
    build:
      context: ./apps/user-client
    container_name: ai_kb_v2_user_client
    environment:
      VITE_API_BASE_URL: /api
    ports:
      - "5173:5173"
    depends_on:
      - backend

  admin-web:
    build:
      context: ./apps/admin-web
    container_name: ai_kb_v2_admin_web
    environment:
      VITE_API_BASE_URL: /api
    ports:
      - "5174:5174"
    depends_on:
      - backend

  gateway:
    image: nginx:1.27-alpine
    container_name: ai_kb_v2_gateway
    ports:
      - "8080:80"
    volumes:
      - ./deploy/gateway.conf:/etc/nginx/conf.d/default.conf:ro
    depends_on:
      - backend
      - user-client
      - admin-web

volumes:
  ai_kb_v2_mysql_data:
  ai_kb_v2_redis_data:
