# =============================================================================
# Reusable Workflow: Client 全平台构建
#
# Tauri 桌面客户端多平台构建,支持两种模式:
#   - preview: PR 预览,产物上传为 artifact(保留 5 天)
#   - release: 正式发布,产物上传到 GitHub Release
# 平台: macOS (arm64/x64), Windows (x64), Linux (x64)
# 产物: DMG, App, MSI, EXE, AppImage, Deb
# =============================================================================

name: 'Reusable: Client Build'

on:
  workflow_call:
    inputs:
      mode:
        description: 'preview: upload artifacts | release: upload to GitHub Release'
        required: false
        type: string
        default: 'preview'
      client_version:
        description: 'Client version for GitHub Release tag (required for release mode)'
        required: false
        type: string
        default: ''

jobs:
  client-build:
    runs-on: ${{ matrix.platform }}
    continue-on-error: ${{ inputs.mode == 'preview' }}
    timeout-minutes: 60
    strategy:
      fail-fast: false
      max-parallel: 4
      matrix:
        include:
          - platform: macos-latest
            target: aarch64-apple-darwin
            platform_name: macos
            arch: arm64
            args: '--target aarch64-apple-darwin'
          - platform: macos-latest
            target: x86_64-apple-darwin
            platform_name: macos
            arch: x64
            args: '--target x86_64-apple-darwin'
          - platform: windows-latest
            target: x86_64-pc-windows-msvc
            platform_name: windows
            arch: x64
            args: '--target x86_64-pc-windows-msvc'
          - platform: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            platform_name: linux
            arch: x64
            args: '--target x86_64-unknown-linux-gnu'

    steps:
      - uses: actions/checkout@v6

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version-file: .node-version
          cache: 'yarn'

      - name: Setup Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          target: ${{ matrix.target }}
          toolchain: stable

      # Preview 模式: 使用独立的 rust-cache(只读,不保存新缓存)
      - name: Cache Rust Dependencies (Preview)
        if: inputs.mode == 'preview'
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: packages/client/src-tauri -> target
          save-if: false

      - name: Install Linux Dependencies
        if: matrix.platform == 'ubuntu-latest'
        run: |
          sudo apt-get update
          sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf

      - name: Install Dependencies
        run: yarn --network-timeout 100000

      - name: Build Cherry Markdown
        run: yarn build

      - name: Build Tauri Client
        run: yarn build:client ${{ matrix.args }}

      # =========================================================================
      # Preview 模式: 上传 artifact(保留 5 天)
      # =========================================================================

      # macOS: Upload DMG
      - name: Upload DMG
        if: inputs.mode == 'preview' && matrix.platform_name == 'macos'
        uses: actions/upload-artifact@v4
        with:
          name: client-${{ matrix.platform_name }}-${{ matrix.arch }}-dmg
          path: ./packages/client/src-tauri/target/${{ matrix.target }}/release/bundle/dmg/*.dmg
          retention-days: 5
          if-no-files-found: warn

      # macOS: Upload App
      - name: Upload App
        if: inputs.mode == 'preview' && matrix.platform_name == 'macos'
        uses: actions/upload-artifact@v4
        with:
          name: client-${{ matrix.platform_name }}-${{ matrix.arch }}-app
          path: ./packages/client/src-tauri/target/${{ matrix.target }}/release/bundle/macos/*.app
          retention-days: 5
          if-no-files-found: warn

      # Windows: Upload MSI
      - name: Upload MSI
        if: inputs.mode == 'preview' && matrix.platform_name == 'windows'
        uses: actions/upload-artifact@v4
        with:
          name: client-${{ matrix.platform_name }}-${{ matrix.arch }}-msi
          path: ./packages/client/src-tauri/target/${{ matrix.target }}/release/bundle/msi/*.msi
          retention-days: 5
          if-no-files-found: warn

      # Windows: Upload NSIS Exe
      - name: Upload NSIS Installer
        if: inputs.mode == 'preview' && matrix.platform_name == 'windows'
        uses: actions/upload-artifact@v4
        with:
          name: client-${{ matrix.platform_name }}-${{ matrix.arch }}-exe
          path: ./packages/client/src-tauri/target/${{ matrix.target }}/release/bundle/nsis/*.exe
          retention-days: 5
          if-no-files-found: warn

      # Linux: Upload AppImage
      - name: Upload AppImage
        if: inputs.mode == 'preview' && matrix.platform_name == 'linux'
        uses: actions/upload-artifact@v4
        with:
          name: client-${{ matrix.platform_name }}-${{ matrix.arch }}-appimage
          path: ./packages/client/src-tauri/target/${{ matrix.target }}/release/bundle/appimage/*.AppImage
          retention-days: 5
          if-no-files-found: warn

      # Linux: Upload Deb
      - name: Upload Deb
        if: inputs.mode == 'preview' && matrix.platform_name == 'linux'
        uses: actions/upload-artifact@v4
        with:
          name: client-${{ matrix.platform_name }}-${{ matrix.arch }}-deb
          path: ./packages/client/src-tauri/target/${{ matrix.target }}/release/bundle/deb/*.deb
          retention-days: 5
          if-no-files-found: warn

      # =========================================================================
      # Release 模式: 上传到 GitHub Release
      # =========================================================================

      # macOS: Upload Release Assets
      - name: Upload macOS Release Asset
        if: inputs.mode == 'release' && matrix.platform_name == 'macos'
        uses: AButler/upload-release-assets@v3.0
        with:
          files: |
            packages/client/src-tauri/target/${{ matrix.target }}/release/bundle/dmg/*.dmg
          release-tag: '@cherry-markdown/client@${{ inputs.client_version }}'
          repo-token: ${{ secrets.GITHUB_TOKEN }}

      # Windows: Upload Release Assets
      - name: Upload Windows Release Asset
        if: inputs.mode == 'release' && matrix.platform_name == 'windows'
        uses: AButler/upload-release-assets@v3.0
        with:
          files: |
            packages/client/src-tauri/target/${{ matrix.target }}/release/bundle/msi/*.msi;
            packages/client/src-tauri/target/${{ matrix.target }}/release/bundle/nsis/*.exe
          release-tag: '@cherry-markdown/client@${{ inputs.client_version }}'
          repo-token: ${{ secrets.GITHUB_TOKEN }}

      # Linux: Upload Release Assets
      - name: Upload Linux Release Asset
        if: inputs.mode == 'release' && matrix.platform_name == 'linux'
        uses: AButler/upload-release-assets@v3.0
        with:
          files: |
            packages/client/src-tauri/target/${{ matrix.target }}/release/bundle/deb/*.deb;
            packages/client/src-tauri/target/${{ matrix.target }}/release/bundle/appimage/*.AppImage
          release-tag: '@cherry-markdown/client@${{ inputs.client_version }}'
          repo-token: ${{ secrets.GITHUB_TOKEN }}