name: Bindings Python

on:
  pull_request:
    branches:
      - main
    paths:
      - "src/**"
      - ".github/workflows/bindings.python.yml"
  workflow_call:
    inputs:
      tag:
        description: Tag to release
        required: true
        type: string

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
  cancel-in-progress: true

jobs:
  linux:
    runs-on: [self-hosted, "${{ matrix.runner }}", Linux, 8c16g, gcp]
    strategy:
      matrix:
        include:
          - { arch: x86_64, runner: X64 }
          # - { arch: aarch64, runner: ARM64 }
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: ./.github/actions/build_bindings_python
        with:
          target: ${{ matrix.arch }}-unknown-linux-gnu
          version: ${{ inputs.tag }}
      - name: upload
        if: inputs.tag
        uses: actions/upload-artifact@v4
        with:
          name: python-linux-${{ matrix.arch }}
          path: src/bendpy/dist/*.whl

  macos:
    if: inputs.tag
    runs-on: macos-latest
    strategy:
      matrix:
        arch:
          - aarch64
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: ./.github/actions/build_bindings_python
        with:
          target: ${{ matrix.arch }}-apple-darwin
          version: ${{ inputs.tag }}
      - name: upload
        if: inputs.tag
        uses: actions/upload-artifact@v4
        with:
          name: python-macos-${{ matrix.arch }}
          path: src/bendpy/dist/*.whl

  publish:
    if: inputs.tag
    name: Publish
    needs: [linux, macos]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/download-artifact@v4
        with:
          pattern: python-*
          merge-multiple: true
          path: src/bendpy/dist
      - name: Publish to PyPI
        timeout-minutes: 10
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          skip-existing: true
          packages-dir: src/bendpy/dist