name: Changelog Validation

on:
  pull_request:
    branches: [main]
    paths:
      - 'CHANGELOG.md'
  workflow_dispatch:
    inputs:
      version:
        description: 'Version to validate (e.g., 0.4.6)'
        required: true
        type: string

defaults:
  run:
    # to fail on error in multiline statements (-e), in pipes (-o pipefail), and on unset variables (-u).
    shell: bash -euo pipefail {0}

jobs:
  validate-changelog:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          filter: blob:none
          fetch-depth: 0

      - name: Get version to validate
        id: version
        run: |
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            version="${{ inputs.version }}"
          else
            # For PRs, extract the latest version from changelog
            version=$(grep -E "^## \[[0-9]+\.[0-9]+\.[0-9]+\]" CHANGELOG.md | head -1 | sed -E 's/^## \[([0-9]+\.[0-9]+\.[0-9]+)\].*/\1/')
            if [ -z "$version" ]; then
              echo "ERROR: Could not extract version from CHANGELOG.md"
              exit 1
            fi
          fi
          echo "version=$version" >> $GITHUB_OUTPUT
          echo "Validating changelog for version: $version"

      - name: Validate changelog entry
        run: |
          version="${{ steps.version.outputs.version }}"
          changelog_file="CHANGELOG.md"

          echo "Validating changelog entry for version $version..."

          # Check if changelog exists
          if [ ! -f "$changelog_file" ]; then
            echo "❌ ERROR: CHANGELOG.md not found"
            exit 1
          fi

          # Check if there's a section for this version
          if ! grep -q "^## \[$version\]" "$changelog_file"; then
            echo "❌ ERROR: No changelog entry found for version $version"
            echo "Expected format: ## [$version] - YYYY-MM-DD"
            echo ""
            echo "Please add a changelog entry like this:"
            echo "## [$version] - $(date +%Y-%m-%d)"
            echo ""
            echo "- Add your changes here"
            echo "- Use bullet points for each change"
            echo "- Be descriptive about what changed"
            exit 1
          fi

          # Extract the content for this version
          version_content=$(sed -n "/^## \[$version\]/,/^## \[/p" "$changelog_file" | sed '$d')

          # Check if it has a proper date format
          if ! echo "$version_content" | head -1 | grep -q "## \[$version\] - [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}"; then
            echo "❌ ERROR: Changelog entry for version $version missing proper date format"
            echo "Expected format: ## [$version] - YYYY-MM-DD"
            echo "Current format: $(echo "$version_content" | head -1)"
            exit 1
          fi

          # Extract just the content (skip header and empty lines)
          content_lines=$(echo "$version_content" | tail -n +2 | sed '/^$/d')

          # Check if the content has any meaningful text
          if [ -z "$content_lines" ]; then
            echo "❌ ERROR: Changelog entry for version $version is empty"
            echo "Please add actual change descriptions"
            exit 1
          fi

          # Check for placeholder content (just a single dash)
          if [ "$content_lines" = "-" ]; then
            echo "❌ ERROR: Changelog entry for version $version contains only placeholder dash"
            echo "Please replace placeholder with actual change descriptions"
            exit 1
          fi

          # Count meaningful entries (lines with actual content)
          entry_count=$(echo "$content_lines" | wc -l)

          echo "✅ Changelog validation passed for version $version"
          echo "   - Found $entry_count changelog entries"
          echo "   - Proper date format confirmed"
          echo "   - Content validation passed"

      - name: Show changelog entry
        run: |
          version="${{ steps.version.outputs.version }}"
          echo "📋 Changelog entry for version $version:"
          echo "----------------------------------------"
          sed -n "/^## \[$version\]/,/^## \[/p" CHANGELOG.md | sed '$d'
          echo "----------------------------------------"
