name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  # ── Build binary wheels for every target ──────────────────────────────
  build-wheels:
    name: Build wheels (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # ── Linux (glibc / manylinux) ──
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu

          # ── Linux (musl / musllinux) ──
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
          - os: ubuntu-latest
            target: aarch64-unknown-linux-musl

          # ── macOS ──
          - os: macos-latest
            target: x86_64-apple-darwin
          - os: macos-14
            target: aarch64-apple-darwin

          # ── Windows ──
          - os: windows-latest
            target: x86_64-pc-windows-msvc

    steps:
      - uses: actions/checkout@v4

      - name: Set up QEMU (for aarch64 cross-compilation)
        if: contains(matrix.target, 'aarch64') && runner.os == 'Linux'
        uses: docker/setup-qemu-action@v3
        with:
          platforms: arm64

      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --strip --out dist --interpreter 3.12 3.13
          sccache: 'true'
          manylinux: auto
        env:
          CFLAGS_aarch64_unknown_linux_gnu: "-march=armv8-a -D__ARM_ARCH=8"
          CFLAGS_aarch64_unknown_linux_musl: "-march=armv8-a -D__ARM_ARCH=8"

      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-${{ matrix.target }}
          path: dist

  # ── Build source distribution ─────────────────────────────────────────
  build-sdist:
    name: Build sdist
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Build sdist
        uses: PyO3/maturin-action@v1
        with:
          command: sdist
          args: --out dist

      - name: Upload sdist
        uses: actions/upload-artifact@v4
        with:
          name: wheels-sdist
          path: dist

  # ── Publish everything to PyPI ────────────────────────────────────────
  publish:
    name: Publish to PyPI
    runs-on: ubuntu-latest
    needs: [build-wheels, build-sdist]
    environment: pypi
    permissions:
      id-token: write
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: wheels-*
          merge-multiple: true
          path: dist

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
