name: Python Wheels

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      tag:
        description: Release tag to upload wheels to (for example v0.1.2)
        required: true
        type: string

permissions:
  contents: write

jobs:
  build-wheels:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-22.04
            target: x86_64
          - os: windows-latest
            target: x86_64-pc-windows-msvc
          - os: macos-13
            target: x86_64-apple-darwin
          - os: macos-14
            target: aarch64-apple-darwin

    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout source
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event_name == 'release' && github.event.release.tag_name || inputs.tag }}

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

      - name: Build Linux wheels (manylinux)
        if: matrix.os == 'ubuntu-22.04'
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          manylinux: manylinux2014
          command: build
          args: --release --features python --out dist --interpreter python3.11

      - name: Build Windows/macOS wheels
        if: matrix.os != 'ubuntu-22.04'
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          command: build
          args: --release --features python --out dist --interpreter python3.11

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

  upload-release-assets:
    needs: build-wheels
    runs-on: ubuntu-22.04
    env:
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      TAG: ${{ github.event_name == 'release' && github.event.release.tag_name || inputs.tag }}

    steps:
      - name: Download wheel artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: wheels-*
          path: dist
          merge-multiple: true

      - name: Upload wheels to GitHub Release
        run: gh release upload "$TAG" dist/*.whl --clobber

  publish-pypi:
    needs: build-wheels
    runs-on: ubuntu-22.04
    if: github.event_name == 'release' && github.event.release.prerelease == false && secrets.PYPI_API_TOKEN != ''

    steps:
      - name: Download wheel artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: wheels-*
          path: dist
          merge-multiple: true

      - name: Publish wheels to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}
          packages-dir: dist
