name: publish-pypi

on:
  push:
    tags:
      - "python-v*"

permissions:
  contents: read

jobs:
  linux:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target: [x86_64, aarch64]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist --find-interpreter
          manylinux: auto
          working-directory: rankops-python
      - uses: actions/upload-artifact@v4
        with:
          name: wheel-linux-${{ matrix.target }}
          path: rankops-python/dist/*.whl

  macos:
    runs-on: macos-latest
    strategy:
      matrix:
        target: [x86_64, aarch64]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}-apple-darwin
          args: --release --out dist --find-interpreter
          working-directory: rankops-python
      - uses: actions/upload-artifact@v4
        with:
          name: wheel-macos-${{ matrix.target }}
          path: rankops-python/dist/*.whl

  windows:
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          architecture: x64
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          args: --release --out dist --find-interpreter
          working-directory: rankops-python
      - uses: actions/upload-artifact@v4
        with:
          name: wheel-windows-x64
          path: rankops-python/dist/*.whl

  publish:
    needs: [linux, macos, windows]
    runs-on: ubuntu-latest
    environment: pypi
    permissions:
      id-token: write
    steps:
      - uses: actions/download-artifact@v4
        with:
          path: dist
          merge-multiple: true
      - uses: pypa/gh-action-pypi-publish@release/v1
        with:
          packages-dir: dist/
