name: 构建测试

on:
  push:
    branches:
      - main
      - dev
  pull_request:
    branches:
      - main
  workflow_dispatch:

env:
  NODE_VERSION: "24"

jobs:
  # ==================== Windows 构建 ====================
  build-Windows:
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x64
            name: x64
          - arch: arm64
            name: ARM64

    runs-on: windows-latest
    name: 构建 Windows (${{ matrix.name }})

    steps:
      - name: 检出代码
        uses: actions/checkout@v4

      - name: 配置 Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: 安装依赖
        run: yarn install --frozen-lockfile

      - name: 打包 Windows 安装程序
        run: yarn dist:win --${{ matrix.arch }}
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: 上传构建产物
        uses: actions/upload-artifact@v4
        with:
          name: windows-${{ matrix.arch }}
          path: dist/*.exe
          retention-days: 7

  # ==================== macOS 构建 ====================
  build-macOS:
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: arm64
            os: macos-latest
            name: Apple Silicon
          - arch: x64
            os: macos-latest
            name: Intel

    runs-on: ${{ matrix.os }}
    name: 构建 macOS (${{ matrix.name }})

    steps:
      - name: 检出代码
        uses: actions/checkout@v4

      - name: 配置 Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: 安装依赖
        run: yarn install --frozen-lockfile
        env:
          npm_config_arch: ${{ matrix.arch }}

      - name: 安装目标平台 sharp
        if: matrix.arch == 'x64'
        run: |
          npm install --no-save --ignore-scripts --force @img/sharp-darwin-x64 @img/sharp-libvips-darwin-x64

      - name: 打包 macOS 安装程序
        run: yarn dist:mac --${{ matrix.arch }}
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: 上传构建产物
        uses: actions/upload-artifact@v4
        with:
          name: macos-${{ matrix.arch }}
          path: dist/*.dmg
          retention-days: 7

  # ==================== Linux 构建 ====================
  build-Linux:
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x64
            name: x64
          - arch: arm64
            name: ARM64

    runs-on: ubuntu-latest
    name: 构建 Linux (${{ matrix.name }})

    steps:
      - name: 检出代码
        uses: actions/checkout@v4

      - name: 配置 Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: 安装依赖
        run: yarn install --frozen-lockfile
        env:
          npm_config_arch: ${{ matrix.arch }}

      - name: 安装目标平台 sharp
        if: matrix.arch == 'arm64'
        run: |
          npm install --no-save --ignore-scripts --force @img/sharp-linux-arm64 @img/sharp-libvips-linux-arm64

      - name: 打包 Linux 安装程序
        run: yarn dist:linux --${{ matrix.arch }}
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: 上传构建产物
        uses: actions/upload-artifact@v4
        with:
          name: linux-${{ matrix.arch }}
          path: dist/*.AppImage
          retention-days: 7

  # ==================== 构建摘要 ====================
  summary:
    needs: [build-Windows, build-macOS, build-Linux]
    runs-on: ubuntu-latest
    name: 构建摘要
    if: always()

    steps:
      - name: 输出构建结果
        run: |
          echo "===== 构建测试完成 ====="
          echo "Windows x64: ${{ needs.build-Windows.result }}"
          echo "Windows arm64: ${{ needs.build-Windows.result }}"
          echo "macOS Apple Silicon: ${{ needs.build-macOS.result }}"
          echo "macOS Intel: ${{ needs.build-macOS.result }}"
          echo "Linux x64: ${{ needs.build-Linux.result }}"
          echo "Linux arm64: ${{ needs.build-Linux.result }}"
          echo ""
          echo "构建产物将保留 7 天，可在 Actions 页面下载。"
