services:
  mysql:
    image: mysql:8.4
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:?必须配置 MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ai_knowledge_base_v2
      MYSQL_USER: ai_kb
      MYSQL_PASSWORD: ${MYSQL_PASSWORD:?必须配置 MYSQL_PASSWORD}
      TZ: Asia/Shanghai
    command: ["--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"]
    volumes:
      - ai_kb_v2_mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -uroot -p$$MYSQL_ROOT_PASSWORD --silent"]
      interval: 10s
      timeout: 5s
      retries: 20

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    environment:
      REDIS_PASSWORD: ${REDIS_PASSWORD:?必须配置 REDIS_PASSWORD}
    command: ["redis-server", "--appendonly", "yes", "--requirepass", "${REDIS_PASSWORD:?必须配置 REDIS_PASSWORD}"]
    volumes:
      - ai_kb_v2_redis_data:/data
    healthcheck:
      test: ["CMD-SHELL", "redis-cli -a $$REDIS_PASSWORD ping | grep PONG"]
      interval: 10s
      timeout: 5s
      retries: 20

  backend:
    build:
      context: ./apps/backend
    restart: unless-stopped
    environment:
      APP_ENV: production
      DEBUG: "false"
      DATABASE_URL: ${DATABASE_URL:?必须配置经过 URL 编码的 DATABASE_URL}
      REDIS_URL: ${REDIS_URL:?必须配置经过 URL 编码的 REDIS_URL}
      READINESS_REQUIRES_REDIS: "true"
      LOG_LEVEL: INFO
      BACKEND_WORKERS: ${BACKEND_WORKERS:-4}
      DB_POOL_SIZE: ${DB_POOL_SIZE:-20}
      DB_MAX_OVERFLOW: ${DB_MAX_OVERFLOW:-20}
      JWT_SECRET_KEY: ${JWT_SECRET_KEY:?必须配置 JWT_SECRET_KEY}
      CONFIG_ENCRYPTION_KEY: ${CONFIG_ENCRYPTION_KEY:?必须配置 CONFIG_ENCRYPTION_KEY}
      MOCK_SMS_ENABLED: "false"
      MOCK_RAG_ENABLED: "false"
      MOCK_MODEL_ENABLED: "false"
      FEISHU_MOCK_ENABLED: "false"
      BOOTSTRAP_ADMIN_USERNAME: ${BOOTSTRAP_ADMIN_USERNAME:?必须配置 BOOTSTRAP_ADMIN_USERNAME}
      BOOTSTRAP_ADMIN_PASSWORD: ${BOOTSTRAP_ADMIN_PASSWORD:?必须配置 BOOTSTRAP_ADMIN_PASSWORD}
      BOOTSTRAP_ADMIN_NAME: ${BOOTSTRAP_ADMIN_NAME:-系统管理员}
      AUTO_CREATE_TABLES: "false"
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8100/api/ready', timeout=3)"]
      interval: 15s
      timeout: 5s
      retries: 10
      start_period: 30s

  user-client:
    build:
      context: ./apps/user-client
      dockerfile: Dockerfile.prod
    restart: unless-stopped

  admin-web:
    build:
      context: ./apps/admin-web
      dockerfile: Dockerfile.prod
    restart: unless-stopped

  gateway:
    image: nginx:1.27-alpine
    restart: unless-stopped
    ports:
      - "${GATEWAY_PORT:-8080}:80"
    volumes:
      - ./deploy/gateway.prod.conf:/etc/nginx/conf.d/default.conf:ro
    depends_on:
      backend:
        condition: service_healthy
      user-client:
        condition: service_started
      admin-web:
        condition: service_started

volumes:
  ai_kb_v2_mysql_data:
  ai_kb_v2_redis_data:
