name: Auto Release

on:
  push:
    branches: [main]

permissions:
  contents: write
  pull-requests: write

jobs:
  release:
    name: Auto Release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Get version
        id: version
        run: |
          VERSION=$(node -p "require('./package.json').version")
          echo "version=v${VERSION}" >> $GITHUB_OUTPUT
          echo "Release v${VERSION}"

      - name: Check GitHub Release exists
        id: check
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          VERSION="${{ steps.version.outputs.version }}"
          if gh release view "$VERSION" --json id 2>/dev/null; then
            echo "exists=true" >> $GITHUB_OUTPUT
            echo "Release $VERSION already exists — skipping"
          else
            echo "exists=false" >> $GITHUB_OUTPUT
            echo "Release $VERSION does not exist — creating"
          fi

      - name: Generate release notes
        if: steps.check.outputs.exists == 'false'
        run: |
          VERSION="${{ steps.version.outputs.version }}"
          awk -v ver="$VERSION" '
            /^## \['"$VERSION"'\]/ { found=1; next }
            found && /^## \[/ { exit }
            found { print }
          ' CHANGELOG.md > release_notes.md
          if [ ! -s release_notes.md ]; then
            echo "See [CHANGELOG.md](CHANGELOG.md) for details." > release_notes.md
          fi

      - name: Create GitHub Release
        if: steps.check.outputs.exists == 'false'
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          VERSION="${{ steps.version.outputs.version }}"
          gh release create "$VERSION" \
            --title "Pantheon ${VERSION}" \
            --notes-file release_notes.md
          echo "✅ Published Pantheon ${VERSION}"
