version: "3.8"

services:
  mysql:
    image: mysql:8.0
    container_name: staff-mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-rootpass}
      MYSQL_DATABASE: staffapp
      MYSQL_USER: staffapp
      MYSQL_PASSWORD: ${MYSQL_PASSWORD:-staffpass}
      TZ: Asia/Shanghai
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --default-authentication-plugin=mysql_native_password
    volumes:
      - mysql_data:/var/lib/mysql
    ports:
      - "127.0.0.1:3307:3306"   # 仅本机可见，不对外
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD:-rootpass}"]
      interval: 10s
      timeout: 5s
      retries: 10

  app:
    build: .
    container_name: staff-app
    restart: always
    depends_on:
      mysql:
        condition: service_healthy
    environment:
      SECRET_KEY: ${SECRET_KEY:-please-change-me}
      DATABASE_URL: mysql+pymysql://staffapp:${MYSQL_PASSWORD:-staffpass}@mysql:3306/staffapp?charset=utf8mb4
      SUPER_PHONE: ${SUPER_PHONE:-13800000000}
      SUPER_PASSWORD: ${SUPER_PASSWORD:-admin123}
      CORS_ORIGINS: ${CORS_ORIGINS:-}
      XYX_DIR: /app/xyx-src          # xyx 新营销看板目录（销售看板：上传xls重生成kb.html）
    volumes:
      - ./uploads:/app/uploads
      - ../xyxkb/xyx:/app/xyx-src    # xyx 目录（与 staff-platform 平级 = /home/ubuntu/html/data/xyxkb/xyx）
    ports:
      - "127.0.0.1:8000:8000"   # 由 Nginx 反代，不直接对外
    command: >
      sh -c "flask migrate-gamecode && flask initdb-seed && gunicorn -w 4 -b 0.0.0.0:8000 --access-logfile - --error-logfile - wsgi:app"

volumes:
  mysql_data:
