name: Wheels

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

permissions:
  contents: read

jobs:
  build-wheels:
    name: ${{ matrix.os_name }}
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os_name: linux
            runner: ubuntu-latest
            rust_target: x86_64-unknown-linux-gnu
          - os_name: linux-arm64
            runner: ubuntu-24.04-arm
            rust_target: aarch64-unknown-linux-gnu
          - os_name: windows-amd64
            runner: windows-latest
            rust_target: x86_64-pc-windows-msvc
          - os_name: windows-arm64
            runner: windows-11-arm
            rust_target: aarch64-pc-windows-msvc
            python_versions: "3.11 3.12 3.13 3.14 3.14t"
          - os_name: macos-intel
            runner: macos-15-intel
            rust_target: x86_64-apple-darwin
          - os_name: macos-arm64
            runner: macos-15
            rust_target: aarch64-apple-darwin

    steps:
      - name: Check out repository
        uses: actions/checkout@v6
        with:
          persist-credentials: false

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: "3.13"

      - name: Set up uv
        uses: astral-sh/setup-uv@v6

      - name: Set up Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: 1.94.0

      - name: Cache Cargo registry and target
        uses: Swatinem/rust-cache@v2

      - name: Build wheels
        shell: bash
        env:
          RSLOOP_PYTHON_VERSIONS: ${{ matrix.python_versions }}
        run: scripts/build-wheels.sh --out dist --target "${{ matrix.rust_target }}"

      - name: Upload wheel artifacts
        uses: actions/upload-artifact@v4
        with:
          name: wheels-${{ matrix.os_name }}
          path: dist/*.whl
          if-no-files-found: error

  build-sdist:
    name: sdist
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository
        uses: actions/checkout@v6
        with:
          persist-credentials: false

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: "3.13"

      - name: Set up uv
        uses: astral-sh/setup-uv@v6

      - name: Set up Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: 1.94.0

      - name: Cache Cargo registry and target
        uses: Swatinem/rust-cache@v2

      - name: Build source distribution
        run: uv run --no-project --with maturin maturin sdist --out dist

      - name: Upload source distribution
        uses: actions/upload-artifact@v4
        with:
          name: sdist
          path: dist/*.tar.gz
          if-no-files-found: error

  publish-pypi:
    name: publish
    if: startsWith(github.ref, 'refs/tags/v')
    needs:
      - build-wheels
      - build-sdist
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
    environment:
      name: pypi
      url: https://pypi.org/project/rsloop/
    steps:
      - name: Download release artifacts
        uses: actions/download-artifact@v4
        with:
          path: dist
          merge-multiple: true

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