name: 发布到 npm

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      version:
        description: '版本号 (例如: 1.0.0)'
        required: true
        default: '1.0.0'

jobs:
  publish:
    runs-on: ubuntu-latest
    
    steps:
    - name: 检出代码
      uses: actions/checkout@v4
      
    - name: 设置 Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '18'
        registry-url: 'https://registry.npmjs.org'
        
    - name: 设置 Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.9'
        
    - name: 安装 Python 依赖
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
        
    - name: 运行测试
      run: |
        python -c "
        import sys
        required_modules = ['fastmcp', 'playwright']
        missing = []
        for module in required_modules:
            try:
                __import__(module)
            except ImportError:
                missing.append(module)
        if missing:
            print(f'❌ 缺少依赖: {missing}')
            sys.exit(1)
        print('✅ 依赖检查通过')
        "
        
    - name: 更新版本号
      if: github.event_name == 'workflow_dispatch'
      run: |
        npm version ${{ github.event.inputs.version }} --no-git-tag-version
        
    - name: 创建 .npmignore
      run: |
        cat > .npmignore << EOF
        # 开发文件
        test_*.py
        debug_*.py
        *.pyc
        __pycache__/
        .pytest_cache/
        .coverage
        
        # Git 文件
        .git/
        .gitignore
        .github/
        
        # IDE 文件
        .vscode/
        .idea/
        *.swp
        *.swo
        
        # 日志文件
        *.log
        logs/
        
        # 临时文件
        tmp/
        temp/
        .tmp/
        
        # 浏览器数据
        browser_data/
        .deps_installed
        
        # 环境文件
        .env
        .env.local
        
        # 文档源文件
        docs/
        DEPLOYMENT_GUIDE.md
        
        # Python 虚拟环境
        venv/
        .venv/
        EOF
        
    - name: 确保启动脚本可执行
      run: chmod +x dist/index.js
      
    - name: 发布到 npm
      run: npm publish
      env:
        NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        
    - name: 创建 GitHub Release
      if: github.event_name == 'push'
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ github.ref }}
        release_name: Release ${{ github.ref }}
        body: |
          ## 🎉 新版本发布
          
          ### 安装方式
          ```bash
          npm install -g xiaohongshu-mcp-server
          ```
          
          ### 在 CherryStudio 中配置
          ```json
          {
            "name": "小红书",
            "command": "xiaohongshu-mcp",
            "args": []
          }
          ```
          
          ### 更新日志
          - 优化了评论发布性能
          - 修复了已知问题
          - 改进了用户体验
          
          查看完整文档：https://www.npmjs.com/package/xiaohongshu-mcp-server
        draft: false
        prerelease: false
