---
name: Build Python Wheels

"on":
  push:
  pull_request:
  schedule:
    - cron: "0 6 * * *"  # Daily 6AM UTC build

jobs:
  build-wheels:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
      fail-fast: true

    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-python@v6
      - name: Install native dependencies (Ubuntu)
        run: |
          sudo apt-get update
          sudo apt-get install -y libgpgme-dev libgpg-error-dev
        if: "matrix.os == 'ubuntu-latest'"
      - name: set up rust
        if: matrix.os != 'ubuntu'
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - name: Install native dependencies (MacOS)
        run: brew install swig gpgme
        if: "matrix.os == 'macos-latest'"
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install setuptools wheel cibuildwheel
      - name: Install gpg on supported platforms
        run: |
          mkdir -p "$HOME/.local/bin"
          cp .github/gpgme-config "$HOME/.local/bin/gpgme-config"
          cp .github/gpg-error-config "$HOME/.local/bin/gpg-error-config"
          echo "$HOME/.local/bin" >> $GITHUB_PATH
          pip install -U gpg
        if: "matrix.os != 'windows-latest'"
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v4
        if: "matrix.os == 'ubuntu-latest'"
      - name: Build wheels
        run: python -m cibuildwheel --output-dir wheelhouse
      - name: Upload wheels
        uses: actions/upload-artifact@v7
        with:
          name: artifact-${{ matrix.os }}
          path: ./wheelhouse/*.whl

  build-sdist:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-python@v6
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install build
      - name: Build sdist
        run: python -m build --sdist
      - name: Upload sdist
        uses: actions/upload-artifact@v7
        with:
          name: artifact-source
          path: ./dist/*.tar.gz

  publish:
    runs-on: ubuntu-latest

    needs:
      - build-sdist
      - build-wheels
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
    permissions:
      id-token: write
    environment:
      name: pypi
      url: https://pypi.org/p/upstream-ontologist
    steps:
      - uses: actions/setup-python@v6

      - name: Download wheels
        uses: actions/download-artifact@v8
        with:
          merge-multiple: true
          patterns: artifact-*
          path: dist
      - name: Publish package distributions to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
