name: 构建扩展程序

on:
  push:
    branches: [ main, dev ]

jobs:
  build-for-chrome:
    name: 构建 Chrome 扩展程序
    runs-on: ubuntu-latest
    env:
      VITE_RUN_ID: ${{ gitea.run_number }}
      VITE_HASH_ID: ${{ gitea.sha }}
    
    steps:
    - uses: actions/checkout@v3
      name: 检出代码
    
    - name: 设置 Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '22'
        
    - name: 安装依赖
      run: npm install
      
    - name: 构建扩展程序
      run: npm run build:chrome
      
    - name: 上传构建工件
      uses: actions/upload-artifact@v3
      with:
        name: chrome-extension
        path: dist/
  
  build-for-firefox:
    name: 构建 Firefox 附加组件
    runs-on: ubuntu-latest
    env:
      VITE_RUN_ID: ${{ gitea.run_number }}
      VITE_HASH_ID: ${{ gitea.sha }}
    
    steps:
    - uses: actions/checkout@v3
      name: 检出代码
    
    - name: 设置 Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '22'
        
    - name: 安装依赖
      run: npm install
      
    - name: 构建扩展程序
      run: npm run build:firefox
      
    - name: 上传构建工件
      uses: actions/upload-artifact@v3
      with:
        name: firefox-addon
        path: dist/

  publish-to-chrome-webstore: 
    name: 发布至 Chrome 应用商店
    runs-on: ubuntu-latest
    needs: build-for-chrome
    # 仅在 main 分支上执行发布
    if: gitea.ref == 'refs/heads/main'
    
    steps:
    - name: 下载构建工件
      uses: actions/download-artifact@v3
      with:
        name: chrome-extension
        path: dist/

    - name: 压缩为 ZIP 文件
      run: |-
        cd dist && zip -r ../msrmod-chrome.zip . && cd ..
        
    - name: 设置 Node.js
      uses: actions/setup-node@v3
      with:
        node-version: "22"

    - name: 安装 Chrome Web Store 上传工具
      run: |-
        npm install -g chrome-webstore-upload-cli

    - name: 上传扩展程序到 Chrome Web Store
      run: |-
        chrome-webstore-upload upload \
          --source msrmod-chrome.zip \
          --extension-id kphoaomopljjiofeaigjmbagmbdaamhh \
          --client-id ${{ secrets.CI_GOOGLE_CLIENT_ID }} \
          --client-secret ${{ secrets.CI_GOOGLE_CLIENT_SECRET }} \
          --refresh-token ${{ secrets.CI_GOOGLE_REFRESH_TOKEN }}

  publish-to-firefox-addons:
    name: 发布至 Firefox 附加组件库
    runs-on: ubuntu-latest
    needs: build-for-firefox
    # 仅在 main 分支上执行发布
    if: gitea.ref == 'refs/heads/main'
    
    steps:
    - name: 下载构建工件
      uses: actions/download-artifact@v3
      with:
        name: firefox-addon
        path: dist/

    - name: 设置 Node.js
      uses: actions/setup-node@v3
      with:
        node-version: "22"

    - name: 安装 web-ext 工具
      run: |-
        npm install -g web-ext

    - name: 上传附加组件到 Firefox Add-ons
      run: |-
        web-ext sign \
          --source-dir dist \
          --artifacts-dir artifacts \
          --api-key ${{ secrets.FIREFOX_API_KEY }} \
          --api-secret ${{ secrets.FIREFOX_API_SECRET }} \
          --channel listed

  publish-to-edge-addons:
    name: 发布至 Edge 附加组件商店
    runs-on: ubuntu-latest
    needs: build-for-chrome
    # 仅在 main 分支上执行发布
    if: gitea.ref == 'refs/heads/main'
    
    steps:
    - name: 下载构建工件
      uses: actions/download-artifact@v3
      with:
        name: chrome-extension
        path: dist/

    - name: 压缩为 ZIP 文件
      run: |-
        cd dist && zip -r ../msrmod-edge.zip . && cd ..
        
    - name: 设置 Node.js
      uses: actions/setup-node@v3
      with:
        node-version: "22"

    - name: 上传扩展程序到 Edge Addons
      uses: wdzeng/edge-addon@v2
      with:
        product-id: d63fd5c5-baba-4fcb-95c8-f11c161f9878
        zip-path: msrmod-edge.zip
        api-key: ${{ secrets.EDGE_CLIENT_SECRET }}
        client-id: ${{ secrets.EDGE_CLIENT_ID }}