name: Build and Release

on:
  push:
    tags:
      - "v*"
  workflow_dispatch:
    inputs:
      version:
        description: "Version number (e.g., 1.0.0)"
        required: false
        type: string

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

  # ==================== 发布 ====================
  release:
    needs: [build-Windows, build-macOS, build-Linux]
    runs-on: ubuntu-latest
    name: 发布到 GitHub
    if: startsWith(github.ref, 'refs/tags/')

    permissions:
      contents: write

    steps:
      - name: 下载所有构建产物
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: 整理文件
        run: |
          mkdir -p dist
          find artifacts -type f \( -name "*.exe" -o -name "*.dmg" -o -name "*.AppImage" \) -exec mv {} dist/ \;

      - name: 显示待发布文件
        run: |
          echo "===== 待发布文件列表 ====="
          ls -la dist/

      - name: 创建 GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          name: ToonFlow ${{ github.ref_name }}
          draft: false
          prerelease: ${{ contains(github.ref_name, 'beta') || contains(github.ref_name, 'alpha') }}
          generate_release_notes: true
          body: |
            ## 📦 下载指南

            | 操作系统 | 架构 | 文件 | 说明 |
            |---------|------|------|------|
            | 🪟 Windows | x64 | `ToonFlow-*-win-x64-setup.exe` | **推荐**，适用于大多数 Windows 电脑 |
            | 🪟 Windows | ARM64 | `ToonFlow-*-win-arm64-setup.exe` | 适用于 ARM 架构 Windows 设备 |
            | 🍎 macOS | Apple Silicon | `ToonFlow-*-mac-arm64.dmg` | 适用于 M1/M2/M3/M4 芯片的 Mac |
            | 🍎 macOS | Intel | `ToonFlow-*-mac-x64.dmg` | 适用于 Intel 芯片的 Mac |
            | 🐧 Linux | x64 | `ToonFlow-*-linux-x86_64.AppImage` | 适用于大多数 Linux 发行版 |
            | 🐧 Linux | ARM64 | `ToonFlow-*-linux-arm64.AppImage` | 适用于 ARM 架构的 Linux 设备 |

            > 💡 **不确定选哪个？** Windows 用户通常选 **win-x64-setup.exe**；Mac 用户查看「关于本机」：M 系列芯片选 **arm64.dmg**，Intel 选 **x64.dmg**。

            ## 🚀 安装说明

            - **Windows**：下载 `.exe` 文件，双击运行安装向导即可。如遇 DLL 缺失，请安装 [VC++ 运行库](https://aka.ms/vs/17/release/vc_redist.x64.exe)。
            - **macOS**：下载 `.dmg` 文件，打开后将应用拖入「应用程序」。首次打开如遇安全提示，前往「系统设置 → 隐私与安全性」允许运行。
            - **Linux**：下载 `.AppImage`，执行 `chmod +x ToonFlow-*.AppImage` 后运行。

            ---

          files: dist/*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: 发布完成
        run: |
          echo "✅ 发布完成！"
          echo "🔗 Release: https://github.com/${{ github.repository }}/releases/tag/${{ github.ref_name }}"
