name: Build Wheels (cibuildwheel)

on:
  push:
    branches:
      - main
      - master
    tags:
      - 'v*'
  pull_request:
    branches:
      - main
      - master
  workflow_dispatch:

env:
  CIBW_BUILD: cp38-* cp39-* cp310-* cp311-* cp312-* cp313-*
  CIBW_SKIP: "*-musllinux_* *-win32 *-manylinux_i686"
  CIBW_ARCHS_MACOS: "x86_64 arm64"
  CIBW_ARCHS_LINUX: "x86_64 aarch64"
  CIBW_ARCHS_WINDOWS: "AMD64"
  # Build with maturin
  CIBW_BEFORE_BUILD: pip install maturin
  CIBW_BUILD_FRONTEND: build

jobs:
  build-wheels-linux:
    name: Build wheels on Linux
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target: [x86_64, aarch64]

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up QEMU
        if: matrix.target == 'aarch64'
        uses: docker/setup-qemu-action@v3
        with:
          platforms: arm64

      - name: Build wheels
        uses: pypa/cibuildwheel@v2.16
        with:
          package-dir: rbufrp
          output-dir: wheelhouse
        env:
          CIBW_ARCHS_LINUX: ${{ matrix.target }}

      - uses: actions/upload-artifact@v4
        with:
          name: wheels-linux-${{ matrix.target }}
          path: ./wheelhouse/*.whl
          if-no-files-found: error

  build-wheels-macos:
    name: Build wheels on macOS
    runs-on: macos-latest
    strategy:
      matrix:
        target: [x86_64, arm64]

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Build wheels
        uses: pypa/cibuildwheel@v2.16
        with:
          package-dir: rbufrp
          output-dir: wheelhouse
        env:
          CIBW_ARCHS_MACOS: ${{ matrix.target }}

      - uses: actions/upload-artifact@v4
        with:
          name: wheels-macos-${{ matrix.target }}
          path: ./wheelhouse/*.whl
          if-no-files-found: error

  build-wheels-windows:
    name: Build wheels on Windows
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Build wheels
        uses: pypa/cibuildwheel@v2.16
        with:
          package-dir: rbufrp
          output-dir: wheelhouse

      - uses: actions/upload-artifact@v4
        with:
          name: wheels-windows-amd64
          path: ./wheelhouse/*.whl
          if-no-files-found: error

  build-sdist:
    name: Build source distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install dependencies
        run: pip install maturin build

      - name: Build sdist
        working-directory: rbufrp
        run: maturin sdist --out dist

      - name: Upload sdist
        uses: actions/upload-artifact@v4
        with:
          name: sdist
          path: rbufrp/dist/*.tar.gz
          if-no-files-found: error

  publish-to-github-release:
    name: Publish to GitHub Release
    needs: [build-wheels-linux, build-wheels-macos, build-wheels-windows, build-sdist]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    permissions:
      contents: write

    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: dist
          merge-multiple: true

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          files: dist/*
          generate_release_notes: true
          draft: false
          prerelease: false
