name: Changelog Check

on:
  pull_request:
    types: [opened, synchronize, reopened]
    paths-ignore:
      - '**.md'
      - 'docs/**'
      - '.github/**'
      - 'package-lock.json'
      - 'yarn.lock'

jobs:
  check-changelog:
    name: Verify Changelog Updated
    runs-on: ubuntu-latest
    
    steps:
      - name: 📥 Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: 📝 Check if CHANGELOG.md was modified
        id: changelog-check
        run: |
          # Get list of changed files in PR
          changed_files=$(git diff --name-only origin/${{ github.base_ref }}...HEAD)
          
          # Check if any source files were changed
          source_changed=false
          for file in $changed_files; do
            if [[ $file == *.js ]] || [[ $file == *.jsx ]] || \
               [[ $file == *.ts ]] || [[ $file == *.tsx ]] || \
               [[ $file == *.py ]] || [[ $file == *.go ]]; then
              source_changed=true
              break
            fi
          done
          
          # Check if CHANGELOG.md was updated
          changelog_updated=false
          if echo "$changed_files" | grep -q "CHANGELOG.md"; then
            changelog_updated=true
          fi
          
          echo "source_changed=$source_changed" >> $GITHUB_OUTPUT
          echo "changelog_updated=$changelog_updated" >> $GITHUB_OUTPUT

      - name: ⚠️ Fail if changelog not updated
        if: steps.changelog-check.outputs.source_changed == 'true' && steps.changelog-check.outputs.changelog_updated == 'false'
        run: |
          echo "❌ Source files changed but CHANGELOG.md was not updated!"
          echo ""
          echo "Please update docs/CHANGELOG.md with your changes:"
          echo "1. Add your changes under the [Unreleased] section"
          echo "2. Use appropriate category (Added, Fixed, Changed, etc.)"
          echo "3. Include a brief description of what changed"
          echo ""
          echo "Example:"
          echo "## [Unreleased]"
          echo "### Added"
          echo "- New feature description"
          echo "### Fixed"
          echo "- Bug fix description"
          exit 1

      - name: ✅ Changelog check passed
        if: steps.changelog-check.outputs.changelog_updated == 'true' || steps.changelog-check.outputs.source_changed == 'false'
        run: |
          if [ "${{ steps.changelog-check.outputs.changelog_updated }}" == "true" ]; then
            echo "✅ CHANGELOG.md has been updated"
          else
            echo "✅ No source changes requiring changelog update"
          fi

      - name: 💬 Comment on PR
        if: failure()
        uses: actions/github-script@v6
        with:
          script: |
            const comment = `## ⚠️ Changelog Update Required
            
            This PR contains source code changes but the CHANGELOG.md file has not been updated.
            
            Please update \`docs/CHANGELOG.md\` by adding your changes under the \`[Unreleased]\` section:
            
            \`\`\`markdown
            ## [Unreleased]
            
            ### Added
            - Description of new features
            
            ### Fixed
            - Description of bug fixes
            
            ### Changed
            - Description of changes
            \`\`\`
            
            **Categories to use:**
            - \`Added\` - New features
            - \`Changed\` - Changes in existing functionality
            - \`Deprecated\` - Features that will be removed
            - \`Removed\` - Features that were removed
            - \`Fixed\` - Bug fixes
            - \`Security\` - Security patches
            
            This helps maintain a clear history of changes for users and contributors.`;
            
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: comment
            });