name: _build

# Reusable cross-platform napi binding build matrix.
# Called by ci.yml (PRs) and release.yml (after release-plz cuts a tag).
# Each job uploads its `.node` as `bindings-<target>` so downstream jobs
# in the calling workflow can download them with `actions/download-artifact`.

on:
  workflow_call:

env:
  CARGO_TERM_COLOR: always
  CARGO_INCREMENTAL: "0"
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"

jobs:
  napi:
    name: ${{ matrix.target }}
    runs-on: ${{ matrix.host }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - host: macos-latest
            target: aarch64-apple-darwin
          - host: macos-latest
            target: x86_64-apple-darwin
          - host: ubuntu-latest
            target: x86_64-unknown-linux-gnu
          - host: windows-latest
            target: x86_64-pc-windows-msvc
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install Linux build deps
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y cmake clang libasound2-dev pkg-config

      - name: Install Windows build deps
        if: runner.os == 'Windows'
        run: choco install -y cmake --installargs 'ADD_CMAKE_TO_PATH=System'
        shell: pwsh

      - uses: oven-sh/setup-bun@v2
      - uses: Swatinem/rust-cache@v2
        with:
          key: napi-${{ matrix.target }}

      - run: bun install --frozen-lockfile || bun install

      - name: Build .node
        run: bun run build -- --target ${{ matrix.target }}
        shell: bash

      - name: Smoke-test require()
        shell: bash
        run: |
          ls -la *.node
          bun -e '
            const m = require("./index.js");
            const want = ["polishText", "transcribePcm"];
            for (const k of want) {
              if (typeof m[k] !== "function") {
                console.error("missing export:", k);
                process.exit(1);
              }
            }
            console.log("ok:", Object.keys(m));
          '

      - uses: actions/upload-artifact@v4
        with:
          name: bindings-${{ matrix.target }}
          path: "*.node"
          if-no-files-found: error
          retention-days: 7
