name: Publish Python Package to PyPI

on:
  release:
    types: [published]
  workflow_dispatch:  # allows manual trigger from GitHub UI

permissions:
  id-token: write  # needed for OIDC
  contents: read

jobs:
  build-and-publish:
    name: Build and Publish to PyPI
    runs-on: ubuntu-latest
    environment: pypi  # must match the environment set in PyPI Trusted Publisher

    steps:
      - name: Check out code
        uses: actions/checkout@v5

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: '3.11'

      - name: Install build tools
        run: |
          python -m pip install --upgrade pip
          pip install build

      - name: Build the package
        run: python -m build

      - name: Publish to PyPI using Trusted Publisher
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          skip-existing: true  # optional: skip if version already exists