name: Auto Release on Version Change

on:
  push:
    branches: [main]
    paths:
      - 'composer.json'

permissions:
  contents: write

jobs:
  check-version:
    name: Check for version changes
    runs-on: ubuntu-latest
    outputs:
      version-changed: ${{ steps.check.outputs.changed }}
      new-version: ${{ steps.check.outputs.version }}

    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 2

    - name: Check if version changed
      id: check
      run: |
        # Get current version from composer.json
        CURRENT_VERSION=$(jq -r '.version' composer.json)
        echo "Current version: $CURRENT_VERSION"

        # Get previous version from the previous commit
        git checkout HEAD~1 -- composer.json 2>/dev/null || echo "No previous version found"
        PREVIOUS_VERSION=$(jq -r '.version' composer.json 2>/dev/null || echo "none")
        echo "Previous version: $PREVIOUS_VERSION"

        # Restore current composer.json
        git checkout HEAD -- composer.json

        # Check if version changed
        if [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then
          echo "Version changed from $PREVIOUS_VERSION to $CURRENT_VERSION"
          echo "changed=true" >> $GITHUB_OUTPUT
          echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
        else
          echo "Version unchanged"
          echo "changed=false" >> $GITHUB_OUTPUT
          echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
        fi

  create-release:
    name: Create GitHub Release and Tag
    needs: check-version
    if: needs.check-version.outputs.version-changed == 'true'
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Generate release notes
      id: release-notes
      run: |
        VERSION="${{ needs.check-version.outputs.new-version }}"

        # Create release notes
        cat > release_notes.md << EOF
        ## FlowHunt PHP SDK v${VERSION}

        ### 🚀 What's New
        - Release version ${VERSION}
        - Updated package metadata
        - Latest features and improvements

        ### 📦 Installation

        **Composer:**
        \`\`\`bash
        composer require flowhunt/flowhunt-php-sdk:^${VERSION}
        \`\`\`

        **Or add to your composer.json:**
        \`\`\`json
        {
            "require": {
                "flowhunt/flowhunt-php-sdk": "^${VERSION}"
            }
        }
        \`\`\`

        ### 📚 Documentation
        - [Repository](https://github.com/QualityUnit/flowhunt-php-sdk)
        - [API Documentation](https://github.com/QualityUnit/flowhunt-php-sdk/blob/main/README.md)

        ### 🔗 Links
        - [Packagist Package](https://packagist.org/packages/flowhunt/flowhunt-php-sdk)
        - [Issues & Support](https://github.com/QualityUnit/flowhunt-php-sdk/issues)

        ### 📋 Requirements
        - PHP ^8.1
        - Guzzle HTTP ^7.3

        ---
        *This release was automatically generated when version ${VERSION} was pushed to main branch.*
        EOF

        echo "Release notes created for version $VERSION"

    - name: Create Tag and Release
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        VERSION="${{ needs.check-version.outputs.new-version }}"

        echo "Creating tag and release for version: $VERSION"

        # Create and push the tag
        git config user.name "github-actions[bot]"
        git config user.email "github-actions[bot]@users.noreply.github.com"
        git tag "$VERSION"
        git push origin "$VERSION"

        # Create the release
        gh release create "$VERSION" \
          --title "Release v${VERSION}" \
          --notes-file release_notes.md \
          --latest

        echo "✅ Tag $VERSION created and release v$VERSION published successfully!"
