name: Build & Publish

on:
  push:
    tags: ["v*"]
  workflow_dispatch:

permissions:
  contents: read

jobs:
  # ---------------------------------------------------------------
  # Build Python wheels for each OS × Python version
  # ---------------------------------------------------------------
  build:
    name: Build / ${{ matrix.os }} / ${{ matrix.python }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        python: ["3.12", "3.13", "3.14"]
    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4

      - uses: PyO3/maturin-action@v1
        with:
          command: build
          args: --release -o dist -i python${{ matrix.python }}

      - uses: actions/upload-artifact@v4
        with:
          name: wheel-${{ matrix.os }}-py${{ matrix.python }}
          path: dist/*.whl

  # ---------------------------------------------------------------
  # Build source distribution (fallback for unsupported platforms)
  # ---------------------------------------------------------------
  sdist:
    name: Build sdist
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: PyO3/maturin-action@v1
        with:
          command: sdist
          args: -o dist

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

  # ---------------------------------------------------------------
  # Publish to crates.io
  # ---------------------------------------------------------------
  publish-crates:
    name: Publish to crates.io
    needs: [build, sdist]
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable

      - name: Publish to crates.io
        run: cargo publish --allow-dirty
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  # ---------------------------------------------------------------
  # Publish to PyPI
  # ---------------------------------------------------------------
  publish-pypi:
    name: Publish to PyPI
    needs: [build, sdist]
    runs-on: ubuntu-latest
    environment: pypi
    permissions:
      id-token: write

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

      - uses: actions/download-artifact@v4
        with:
          name: sdist
          path: dist

      - uses: pypa/gh-action-pypi-publish@release/v1

  # ---------------------------------------------------------------
  # Create GitHub Release
  # ---------------------------------------------------------------
  release:
    name: GitHub Release
    needs: [build, sdist]
    runs-on: ubuntu-latest
    permissions:
      contents: write

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

      - uses: actions/download-artifact@v4
        with:
          name: sdist
          path: dist

      - uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: dist/*
