name: Auto-tag and GitHub Release

# Triggered when a PR with title starting "release:" is merged into main.
# Creates a git tag and a GitHub Release with notes from CHANGELOG.md.
# The tag push then triggers the existing release.yml (PyPI publish).

on:
  pull_request:
    types: [closed]
    branches: [main]

permissions:
  contents: write
  actions: write

jobs:
  tag-and-release:
    if: >-
      github.event.pull_request.merged == true &&
      startsWith(github.event.pull_request.title, 'release:')
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Extract version from PR title
        id: version
        run: |
          TITLE="${{ github.event.pull_request.title }}"
          VERSION=$(echo "$TITLE" | grep -oP 'v\d+\.\d+\.\d+')
          if [ -z "$VERSION" ]; then
            echo "::error::No version found in PR title: $TITLE"
            exit 1
          fi
          echo "tag=$VERSION" >> "$GITHUB_OUTPUT"
          # Strip 'v' prefix for CHANGELOG lookup
          echo "version=${VERSION#v}" >> "$GITHUB_OUTPUT"
          echo "Detected version: $VERSION"

      - name: Check tag does not already exist
        run: |
          if git rev-parse "${{ steps.version.outputs.tag }}" >/dev/null 2>&1; then
            echo "::error::Tag ${{ steps.version.outputs.tag }} already exists"
            exit 1
          fi

      - name: Extract release notes from CHANGELOG.md
        id: notes
        run: |
          VERSION="${{ steps.version.outputs.version }}"
          # Extract the section for this version (between ## [X.Y.Z] and the next ## [)
          NOTES=$(awk "/^## \[${VERSION}\]/{flag=1; next} /^## \[/{flag=0} flag" CHANGELOG.md)
          if [ -z "$NOTES" ]; then
            NOTES="Release ${VERSION}"
          fi
          # Write to file for gh release create --notes-file
          echo "$NOTES" > /tmp/release-notes.md
          echo "Release notes extracted:"
          cat /tmp/release-notes.md

      - name: Create tag
        run: |
          git tag "${{ steps.version.outputs.tag }}"
          git push origin "${{ steps.version.outputs.tag }}"

      - name: Create GitHub Release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release create "${{ steps.version.outputs.tag }}" \
            --title "${{ steps.version.outputs.tag }}" \
            --notes-file /tmp/release-notes.md

      - name: Trigger PyPI release workflow
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh workflow run release.yml \
            --ref "${{ steps.version.outputs.tag }}" \
            -f tag="${{ steps.version.outputs.tag }}"
