name: Publish
#
# Manual trigger → TestPyPI
# Release trigger → PyPI

on:
  workflow_dispatch:  # Manual trigger for TestPyPI
  release:
    types: [published]  # Automatic trigger for PyPI

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6.0.1

      - name: Install uv
        uses: astral-sh/setup-uv@v7.2.0

      - name: Build package
        run: uv build

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

  publish-testpypi:
    if: github.event_name == 'workflow_dispatch'
    needs: build
    runs-on: ubuntu-latest
    environment: testpypi
    permissions:
      id-token: write
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v7.0.0
        with:
          name: dist
          path: dist/

      - name: Publish to TestPyPI
        uses: pypa/gh-action-pypi-publish@v1.13.0
        with:
          repository-url: https://test.pypi.org/legacy/

  publish-pypi:
    if: github.event_name == 'release'
    needs: build
    runs-on: ubuntu-latest
    environment: pypi
    permissions:
      id-token: write
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v7.0.0
        with:
          name: dist
          path: dist/

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@v1.13.0
