services:
  mysql:
    image: mysql:8.4
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:?请在环境文件中设置 MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE:-koc_loop}
      MYSQL_USER: ${MYSQL_USER:-koc}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD:?请在环境文件中设置 MYSQL_PASSWORD}
      TZ: Asia/Shanghai
    command:
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_0900_ai_ci
      - --default-time-zone=+00:00
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -u root -p$$MYSQL_ROOT_PASSWORD --silent"]
      interval: 10s
      timeout: 5s
      retries: 20
      start_period: 30s

  app:
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    environment:
      MYSQL_HOST: mysql
      MYSQL_PORT: 3306
      MYSQL_DATABASE: ${MYSQL_DATABASE:-koc_loop}
      MYSQL_USER: ${MYSQL_USER:-koc}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      UPLOAD_DIR: /data/koc/uploads
      APP_ORIGIN: ${APP_ORIGIN}
      KOC_PORTAL_URL: ${KOC_PORTAL_URL}
      SUPER_ADMIN_USERNAME: ${SUPER_ADMIN_USERNAME}
      SUPER_ADMIN_PASSWORD: ${SUPER_ADMIN_PASSWORD}
      ADMIN_INTERNAL_TOKEN: ${ADMIN_INTERNAL_TOKEN}
      KOC_MCP_API_KEY: ${KOC_MCP_API_KEY}
      FEISHU_APP_ID: ${FEISHU_APP_ID:-}
      FEISHU_APP_SECRET: ${FEISHU_APP_SECRET:-}
      AI_TOOL_CENTER_MCP_URL: ${AI_TOOL_CENTER_MCP_URL:-}
      AI_TOOL_CENTER_MCP_KEY: ${AI_TOOL_CENTER_MCP_KEY:-}
      ENABLE_SCHEDULER: ${ENABLE_SCHEDULER:-true}
      SEED_DEMO_DATA: ${SEED_DEMO_DATA:-false}
      TZ: Asia/Shanghai
    volumes:
      - upload_data:/data/koc/uploads
    depends_on:
      mysql:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "node -e \"fetch('http://127.0.0.1:3000/api/health').then(r=>{if(!r.ok)process.exit(1)}).catch(()=>process.exit(1))\""]
      interval: 15s
      timeout: 5s
      retries: 10
      start_period: 45s

  nginx:
    build:
      context: .
      dockerfile: deploy/nginx/Dockerfile
    restart: unless-stopped
    ports:
      - "${HTTP_PORT:-80}:80"
    depends_on:
      app:
        condition: service_healthy

volumes:
  mysql_data:
  upload_data:
