# .github/workflows/post-release-generate-blog.yml
# Version: 2.5.2
# Description: Auto-generates changelog + blog post from tag or GitHub release with dry-run safety and badge output
# Author: Ali Kahwaji

name: Auto Generate Blog + Changelog

on:
  release:
    types: [published]
  push:
    tags:
      - 'v*.*.*'
  workflow_dispatch:
    inputs:
      dry_run:
        description: 'Run generator in dry-run mode (no git push)'
        required: false
        default: 'false'

permissions:
  contents: write
  issues: write
  pull-requests: write

jobs:
  generate:
    name: 📓 Generate Blog + Changelog
    runs-on: ubuntu-latest
    outputs:
      blog_generated: ${{ steps.done.outputs.blog_generated }}

    steps:
      - name: 📥 Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: 🛠 Setup Git identity
        run: |
          git config --global user.name "GitHub Actions"
          git config --global user.email "docs@autobot.local"

      - name: 📦 Install dependencies
        run: npm ci

      - name: 🏷 Extract version and dry_run mode
        id: context
        run: |
          TAG=${GITHUB_REF#refs/tags/}
          echo "tag=${TAG}" >> $GITHUB_OUTPUT
          echo "dry_run=${{ github.event.inputs.dry_run == 'true' && 'true' || 'false' }}" >> $GITHUB_OUTPUT

      - name: 📝 Generate release note
        id: note
        run: |
          echo "🚧 Running release note generator..."
          node scripts/generate-release-note.js "${{ steps.context.outputs.tag }}"
        env:
          DRY_RUN: ${{ steps.context.outputs.dry_run }}

      - name: 🔐 Commit blog & changelog
        if: ${{ steps.context.outputs.dry_run != 'true' }}
        uses: stefanzweifel/git-auto-commit-action@v5
        with:
          commit_message: "docs(release): blog + changelog for ${{ steps.context.outputs.tag }}"
          file_pattern: "CHANGELOG.md docs-site/blog/*.md"

      - name: ✅ Output badge result
        id: done
        run: echo "blog_generated=true" >> "$GITHUB_OUTPUT"
