# Copyright (C) 2026 Moko Consulting <hello@mokoconsulting.tech>
#
# This file is part of a Moko Consulting project.
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# FILE INFORMATION
# DEFGROUP: GitHub.Workflow
# INGROUP: MokoStandards.Automation
# REPO: https://github.com/mokoconsulting-tech/MokoStandards
# PATH: /templates/workflows/shared/sync-version-on-merge.yml.template
# VERSION: 04.06.00
# BRIEF: Auto-bump patch version on every push to main and propagate to all file headers
# NOTE: Synced via bulk-repo-sync to .github/workflows/sync-version-on-merge.yml in all governed repos.
#       README.md is the single source of truth for the repository version.

name: Sync Version from README

on:
  push:
    branches:
      - main
      - master
  workflow_dispatch:
    inputs:
      dry_run:
        description: 'Dry run (preview only, no commit)'
        type: boolean
        default: false

permissions:
  contents: write
  issues: write

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  sync-version:
    name: Propagate README version
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
        with:
          token: ${{ secrets.GH_TOKEN || github.token }}
          fetch-depth: 0

      - name: Set up PHP
        uses: shivammathur/setup-php@fcafdd6392932010c2bd5094439b8e33be2a8a09 # v2.37.0
        with:
          php-version: '8.1'
          tools: composer

      - name: Setup MokoStandards tools
        env:
          GH_TOKEN: ${{ secrets.GH_TOKEN || github.token }}
          COMPOSER_AUTH: '{"github-oauth":{"github.com":"${{ secrets.GH_TOKEN || github.token }}"}}'
        run: |
          git clone --depth 1 --branch version/04 --quiet \
            "https://x-access-token:${GH_TOKEN}@github.com/mokoconsulting-tech/MokoStandards.git" \
            /tmp/mokostandards
          cd /tmp/mokostandards
          composer install --no-dev --no-interaction --quiet

      - name: Auto-bump patch version
        if: ${{ github.event_name == 'push' && github.actor != 'github-actions[bot]' }}
        run: |
          if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -q '^README\.md$'; then
            echo "README.md changed in this push — skipping auto-bump"
            exit 0
          fi

          RESULT=$(php /tmp/mokostandards/api/cli/version_bump.php --path .) || {
            echo "⚠️ Could not bump version — skipping"
            exit 0
          }
          echo "Auto-bumping patch: $RESULT"
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add README.md
          git commit -m "chore(version): auto-bump patch ${RESULT} [skip ci]" \
            --author="github-actions[bot] <github-actions[bot]@users.noreply.github.com>"
          git push

      - name: Extract version from README.md
        id: readme_version
        run: |
          git pull --ff-only 2>/dev/null || true
          VERSION=$(php /tmp/mokostandards/api/cli/version_read.php --path . 2>/dev/null)
          if [ -z "$VERSION" ]; then
            echo "⚠️ No VERSION in README.md — skipping propagation"
            echo "skip=true" >> $GITHUB_OUTPUT
            exit 0
          fi
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "skip=false" >> $GITHUB_OUTPUT
          echo "✅ README.md version: $VERSION"

      - name: Run version sync
        if: ${{ steps.readme_version.outputs.skip != 'true' && inputs.dry_run != true }}
        run: |
          php /tmp/mokostandards/api/maintenance/update_version_from_readme.php \
            --path . \
            --create-issue \
            --repo "${{ github.repository }}"
        env:
          GH_TOKEN: ${{ secrets.GH_TOKEN || github.token }}

      - name: Commit updated files
        if: ${{ steps.readme_version.outputs.skip != 'true' && inputs.dry_run != true }}
        run: |
          git pull --ff-only 2>/dev/null || true
          if git diff --quiet; then
            echo "ℹ️ No version changes needed — already up to date"
            exit 0
          fi
          VERSION="${{ steps.readme_version.outputs.version }}"
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add -A
          git commit -m "chore(version): sync badges and headers to ${VERSION} [skip ci]" \
            --author="github-actions[bot] <github-actions[bot]@users.noreply.github.com>"
          git push

      - name: Summary
        run: |
          VERSION="${{ steps.readme_version.outputs.version }}"
          echo "## 📦 Version Sync — ${VERSION}" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "**Source:** \`README.md\` FILE INFORMATION block" >> $GITHUB_STEP_SUMMARY
          echo "**Version:** \`${VERSION}\`" >> $GITHUB_STEP_SUMMARY
