name: Publish entroly-core wheels to PyPI

# Builds multi-platform Rust wheels using maturin and publishes to PyPI.
# This ensures `pip install "entroly[native]"` always gets the latest Rust engine.
#
# Triggers on version tags (entroly-v*) — same as the Docker/PyPI workflow.

on:
  push:
    tags:
      - 'entroly-v*'
  workflow_dispatch:

jobs:
  build-wheels:
    name: Build wheels (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64
          - os: ubuntu-latest
            target: aarch64
          - os: macos-latest
            target: universal2-apple-darwin
          - os: windows-latest
            target: x86_64

    steps:
      - uses: actions/checkout@v4

      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist --manifest-path entroly-core/Cargo.toml
          manylinux: auto

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

  publish-to-pypi:
    name: Publish entroly-core to PyPI
    needs: build-wheels
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/entroly-v')
    permissions:
      id-token: write

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

      - name: List wheels built
        run: ls -la dist/

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          packages-dir: dist/
          skip-existing: true
