name: Create Release

on:
  push:
    tags:
      - 'v*.*.*'

jobs:
  create-release:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    
    - name: Extract version from tag
      id: get_version
      run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
    
    - name: Extract changelog for version
      id: changelog
      run: |
        VERSION=${{ steps.get_version.outputs.VERSION }}
        # Extract the changelog section for this version
        CHANGELOG=$(sed -n "/## \[$VERSION\]/,/## \[/p" CHANGELOG.md | sed '$d' | tail -n +2)
        # Save to file to preserve newlines
        echo "$CHANGELOG" > /tmp/release_notes.md
    
    - name: Create Release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ github.ref }}
        release_name: Release v${{ steps.get_version.outputs.VERSION }}
        body_path: /tmp/release_notes.md
        draft: false
        prerelease: false
