name: Create Release

on:
  push:
    tags:
      - 'v*'

jobs:
  release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract version from tag
        id: version
        run: |
          VERSION=${GITHUB_REF#refs/tags/}
          echo "tag=$VERSION" >> $GITHUB_OUTPUT
          echo "Building release for $VERSION"

      - name: Check if release already exists
        id: check_release
        run: |
          VERSION="${{ steps.version.outputs.tag }}"
          if gh release view "$VERSION" >/dev/null 2>&1; then
            echo "exists=true" >> $GITHUB_OUTPUT
            echo "Release $VERSION already exists, skipping..."
          else
            echo "exists=false" >> $GITHUB_OUTPUT
            echo "Release $VERSION does not exist, proceeding..."
          fi
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Generate release notes
        if: steps.check_release.outputs.exists == 'false'
        run: |
          VERSION="${{ steps.version.outputs.tag }}"
          VERSION_NO_V=${VERSION#v}

          # Find previous tag
          PREVIOUS_TAG=$(git tag -l 'v*' --sort=-version:refname | grep -v "^${VERSION}$" | head -n 1)
          if [ -z "$PREVIOUS_TAG" ]; then
            PREVIOUS_TAG=""
          fi

          # Get commits since previous tag
          if [ -z "$PREVIOUS_TAG" ]; then
            COMMIT_COUNT=$(git rev-list --count HEAD)
            if [ "$COMMIT_COUNT" -gt 20 ]; then
              COMMITS=$(git log --oneline --pretty=format:"- %s" --no-merges HEAD~20..HEAD)
            else
              COMMITS=$(git log --oneline --pretty=format:"- %s" --no-merges)
            fi
          else
            COMMITS=$(git log --oneline --pretty=format:"- %s" --no-merges "$PREVIOUS_TAG"..HEAD)
          fi

          cat > release_notes.md << NOTES_EOF
          ## Install

          \`\`\`bash
          uv tool install specify-cli --from git+https://github.com/github/spec-kit.git@${VERSION}
          specify init my-project
          \`\`\`

          NOTES_EOF

          echo "## What's Changed" >> release_notes.md
          echo "" >> release_notes.md
          echo "$COMMITS" >> release_notes.md

      - name: Create GitHub Release
        if: steps.check_release.outputs.exists == 'false'
        run: |
          VERSION="${{ steps.version.outputs.tag }}"
          VERSION_NO_V=${VERSION#v}
          gh release create "$VERSION" \
            --title "Spec Kit - $VERSION_NO_V" \
            --notes-file release_notes.md
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
