name: Create Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

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

      - name: Generate changelog
        id: changelog
        run: |
          # Get the previous tag
          PREVIOUS_TAG=$(git tag --sort=-version:refname | sed -n '2p')
          CURRENT_TAG=${GITHUB_REF#refs/tags/}
          
          echo "## Changes" > changelog.md
          echo "" >> changelog.md
          
          if [ -n "$PREVIOUS_TAG" ]; then
            echo "### Commits since $PREVIOUS_TAG:" >> changelog.md
            git log --pretty=format:"- %s (%h)" $PREVIOUS_TAG..$CURRENT_TAG >> changelog.md
          else
            echo "### All commits:" >> changelog.md
            git log --pretty=format:"- %s (%h)" $CURRENT_TAG >> changelog.md
          fi
          
          echo "" >> changelog.md
          echo "**Full Changelog**: https://github.com/${{ github.repository }}/compare/$PREVIOUS_TAG...$CURRENT_TAG" >> changelog.md
          
          # Set output for use in release step
          {
            echo 'CHANGELOG<<EOF'
            cat changelog.md
            echo 'EOF'
          } >> $GITHUB_OUTPUT

      - name: Create Release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref_name }}
          release_name: ${{ github.ref_name }}
          body: ${{ steps.changelog.outputs.CHANGELOG }}
          draft: false
          prerelease: ${{ contains(github.ref_name, '-') }}
