# NOTE: PyPI publishing uses OIDC trusted publishing (no API tokens needed).
# To enable, configure a trusted publisher on PyPI and TestPyPI for this repo:
#   PyPI:     https://pypi.org/manage/account/publishing/
#   TestPyPI: https://test.pypi.org/manage/account/publishing/
# Publisher settings: owner=TheKingHippopotamus, repo=GOLEM-3DMCP, workflow=release.yml
# Until configured, the publish-test and publish jobs will fail gracefully
# (continue-on-error: true) and will not block the GitHub Release from being created.
name: Release to PyPI

on:
  push:
    tags: ["v*"]

permissions:
  contents: write
  id-token: write

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Build
        run: |
          pip install build
          python -m build

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: dist
          path: dist/

  github-release:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/download-artifact@v4
        with:
          name: dist
          path: dist/

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: dist/*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  publish-test:
    needs: build
    runs-on: ubuntu-latest
    continue-on-error: true
    environment: test-pypi
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: dist
          path: dist/

      - uses: pypa/gh-action-pypi-publish@release/v1
        with:
          repository-url: https://test.pypi.org/legacy/

  publish:
    needs: build
    runs-on: ubuntu-latest
    continue-on-error: true
    environment: pypi
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: dist
          path: dist/

      - uses: pypa/gh-action-pypi-publish@release/v1
