name: "PR Test Build"

on:
  workflow_dispatch:
    inputs:
      pr_number:
        description: "PR number to build"
        required: true
        type: string

jobs:
  build-test:
    permissions:
      contents: write
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: "macos-26"
            args: "--target aarch64-apple-darwin"
            target: "aarch64-apple-darwin"
          - platform: "macos-latest"
            args: "--target x86_64-apple-darwin"
            target: "x86_64-apple-darwin"
          - platform: "ubuntu-22.04"
            args: "--bundles deb"
            target: "x86_64-unknown-linux-gnu"
          - platform: "ubuntu-24.04"
            args: "--bundles appimage,rpm"
            target: "x86_64-unknown-linux-gnu"
          - platform: "ubuntu-24.04-arm" # Build for ARM64 Linux
            args: "--bundles appimage,deb,rpm"
            target: "aarch64-unknown-linux-gnu"
          - platform: "windows-latest"
            args: ""
            target: "x86_64-pc-windows-msvc"
          - platform: "windows-11-arm"
            args: "--target aarch64-pc-windows-msvc"
            target: "aarch64-pc-windows-msvc"

    uses: ./.github/workflows/build.yml
    with:
      platform: ${{ matrix.platform }}
      target: ${{ matrix.target }}
      build-args: ${{ matrix.args }}
      sign-binaries: true
      asset-prefix: "handy-pr-${{ inputs.pr_number }}"
      upload-artifacts: true
      is-debug-build: ${{ contains(matrix.args, '--debug') }}
      ref: ${{ format('refs/pull/{0}/merge', inputs.pr_number) }}
    secrets: inherit

  comment-on-pr:
    needs: build-test
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - name: Post artifact links to PR
        uses: actions/github-script@v7
        with:
          script: |
            const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: ${{ inputs.pr_number }},
              body: `## 🧪 Test Build Ready\n\nBuild artifacts for PR #${{ inputs.pr_number }} are available for testing.\n\n**[Download artifacts from workflow run](${runUrl})**\n\nArtifacts expire after 30 days.`
            });
