name: Build and Upload Python Package to PyPI

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      testpypi:
        description: "Publish to test pypi"
        required: false
        type: boolean
        default: false

run-name: >
  Build & Publish → ${{ github.event_name == 'release' && 'PyPI' ||
      github.event_name == 'workflow_dispatch' && inputs.testpypi && 'TestPyPI' ||
      'Build Only' }}

permissions:
  contents: read

jobs:
  build:
    name: Build wheels on ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            cibw_archs: "i686,x86_64" # x64 native, i686 over QEMU
            needs_qemu: true

          - os: ubuntu-24.04-arm
            cibw_archs: "aarch64,armv7l" # aarch64
            needs_qemu: false

          - os: windows-latest
            cibw_archs: "x86,AMD64"
            needs_qemu: false

          - os: windows-11-arm
            cibw_archs: "ARM64"
            needs_qemu: false

          - os: macos-15
            cibw_archs: "arm64,universal2"
            needs_qemu: false

          - os: macos-15-intel
            cibw_archs: "x86_64,universal2"
            needs_qemu: false

    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0 # required for setuptools-scm

      - name: Set up QEMU for Emulated Architectures
        if: ${{ matrix.needs_qemu }}
        uses: docker/setup-qemu-action@v3
        with:
          platforms: all

      - name: Install uv
        uses: astral-sh/setup-uv@v7
        with:
          github-token: ${{ github.token }}

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

      - name: Install dependencies
        run: python -m pip install build cibuildwheel
        shell: bash

      - name: Build pure python package
        run: python -m build
        shell: bash

      - name: Build binary python package
        run: |
          python -m build py_ballisticcalc.exts --sdist --outdir dist
          cibuildwheel py_ballisticcalc.exts --output-dir dist --platform auto --archs ${{ matrix.cibw_archs }}
        shell: bash

      - name: list ./dist
        run: ls ./dist
        shell: bash

      - name: Upload artifacts
        uses: actions/upload-artifact@v6
        with:
          name: dist-${{ matrix.os }}
          path: ./dist
          retention-days: 14

  publish:
    needs: build
    runs-on: ubuntu-latest
    environment: ${{ github.event_name == 'release' && 'pypi' || inputs.testpypi && 'testpypi' || 'build' }}
    permissions:
      id-token: write

    steps:
      - name: Download all artifacts dynamically
        uses: actions/download-artifact@v4
        with:
          pattern: dist-*
          path: ./all-dist

      - name: Combine artifacts
        run: |
          mkdir -p dist
          find ./all-dist -type f -exec cp {} dist/ \;
        shell: bash

      - name: list ./dist
        run: ls ./dist
        shell: bash

      - name: Upload artifacts
        uses: actions/upload-artifact@v6
        with:
          name: dist
          path: ./dist
          retention-days: 14

      - name: Publish package distributions to PyPI
        if: ${{ github.event_name == 'release' }}
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          skip-existing: true

      - name: Publish package distributions to TestPyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        if: ${{ github.event_name == 'workflow_dispatch' && inputs.testpypi == true }}
        with:
          skip-existing: true
          repository-url: https://test.pypi.org/legacy/
