name: 2. Release + Publish to PyPI (manual)

on:
  workflow_dispatch:

permissions:
  contents: write
  id-token: write

jobs:
  release-and-publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.13'

      - name: Setup uv
        uses: astral-sh/setup-uv@v5
        with:
          enable-cache: true

      - name: Sync deps and build dist
        shell: bash
        run: |
          set -euo pipefail
          uv python install 3.13
          uv venv --python 3.13
          source .venv/bin/activate
          uv sync --all-groups
          # Build sdist and wheel using hatchling via uv
          uv build
          ls -l dist

      - name: Compute version and tag
        id: v
        shell: bash
        run: |
          set -euo pipefail
          VERSION=$(python -c "import tomllib,sys;print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])")
          TAG="v${VERSION}"
          echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
          echo "tag=${TAG}" >> "$GITHUB_OUTPUT"

      - name: Create GitHub Release and upload artifacts
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ steps.v.outputs.tag }}
          name: ${{ steps.v.outputs.tag }}
          generate_release_notes: true
          files: |
            dist/*.whl
            dist/*.tar.gz

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

