name: CI

on:
  push:
    branches: ['*']
    tags:
      - 'v*'
  pull_request:
    branches: ['*']

jobs:
  build:
    name: Build on ${{ matrix.name }}
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: ubuntu-x64
            runner: ubuntu-latest
          - name: ubuntu-arm64
            runner: ubuntu-24.04-arm
          - name: windows-x64
            runner: windows-latest
          - name: macos-arm64
            runner: macos-15
          - name: macos-x64
            runner: macos-15-intel

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - name: Set up MSBuild
        if: runner.os == 'Windows'
        uses: microsoft/setup-msbuild@v2

      - name: Install build tools (Linux)
        if: runner.os == 'Linux'
        run: sudo apt-get update && sudo apt-get install -y cmake build-essential

      # Cache the libiconv bootstrap prefix and cmake-js build directory to
      # avoid rebuilding external/native dependencies from scratch.
      - name: Cache cmake build
        uses: actions/cache@v4
        id: cmake-cache
        with:
          path: |
            .build-deps
            build
          key: cmake-${{ matrix.name }}-${{ hashFiles('CMakeLists.txt', 'CMake/**', 'src/**', 'scripts/**', 'package.json') }}
          restore-keys: |
            cmake-${{ matrix.name }}-

      - name: Install npm dependencies
        run: npm install --ignore-scripts

      - name: Build TypeScript
        run: npm run build

      - name: Bootstrap libiconv
        shell: bash
        run: |
          npm run setup:libiconv
          prefix="$(node -e "const fs = require('node:fs'); const path = require('node:path'); const { getLibIconvPrefix } = require('./scripts/libiconv'); const prefix = getLibIconvPrefix(); const header = path.join(prefix, 'include', 'iconv.h'); if (!fs.existsSync(header)) { console.error('Expected libiconv header not found at ' + header); process.exit(1); } process.stdout.write(prefix);")"
          echo "Using bootstrapped LIB_ICONV=$prefix"
          echo "LIB_ICONV=$prefix" >> "$GITHUB_ENV"

      - name: Compile native addon
        run: npm run compile -- -l verbose

      - name: Run tests
        run: npm test

      # -----------------------------------------------------------------------
      # The steps below only run when a version tag (v*) is pushed.
      # They create a platform-specific prebuilt binary and upload it as an
      # artifact so the release job can attach it to the GitHub Release.
      # -----------------------------------------------------------------------
      - name: Create prebuilt binary
        if: startsWith(github.ref, 'refs/tags/v')
        run: npm run pack:prebuilds

      - name: Upload prebuilt artifact
        if: startsWith(github.ref, 'refs/tags/v')
        uses: actions/upload-artifact@v4
        with:
          name: prebuilds-${{ matrix.name }}
          path: prebuilds/
          if-no-files-found: error

  # ---------------------------------------------------------------------------
  # Collect all platform prebuilt binaries and attach them to the GitHub
  # Release that triggered this run.
  # ---------------------------------------------------------------------------
  release:
    name: Publish GitHub Release
    needs: build
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    permissions:
      contents: write

    steps:
      - name: Download all prebuild artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: prebuilds-*
          path: prebuilds
          merge-multiple: true

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: prebuilds/**
          generate_release_notes: true
