name: Wheels

on: workflow_dispatch

jobs:
  build_sdist:
    name: Build source distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Build sdist
        run: pipx run build --sdist

      - uses: actions/upload-artifact@v4
        with:
          name: cibw-sdist
          path: dist/*.tar.gz

  build_wheels:
    name: Build wheels on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-22.04
            CIBW_ARCHS: x86_64
          - os: macos-15
            CIBW_ARCHS: arm64
          - os: macos-15-intel
            CIBW_ARCHS: x86_64
          - os: windows-2022
            CIBW_ARCHS: AMD64

    steps:
      - name: Checkout repo with submodules
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

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

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install setuptools setuptools_scm build wheel pytest pytest-cov coveralls

      - name: Build wheels
        uses: pypa/cibuildwheel@v3.3.1
        env:
          CIBW_BUILD: "cp39-* cp310-* cp311-* cp312-* cp313-* cp314-*"
          CIBW_ARCHS: ${{ matrix.CIBW_ARCHS }}
          CIBW_TEST_COMMAND: "pytest {package}/tests"
        with:
          package-dir: .

      - uses: actions/upload-artifact@v4
        with:
          name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
          path: ./wheelhouse/*.whl

  pypi-publish:
    name: Upload release to PyPI
    needs: [build_sdist, build_wheels]
    runs-on: ubuntu-latest
    environment: pypi
    permissions:
      id-token: write
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: cibw-*
          path: dist
          merge-multiple: true

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

