name: Sync MCP registry

# Whenever Razor UI sources change on master, regenerate the MCP
# registry artefacts (registry.json + components-api.json) so the
# committed LLM-facing catalog stays in lockstep with the source
# of truth. Triggers downstream sync-skill-catalog.yml automatically
# when components-api.json changes (which then regenerates the offline
# skill reference). End result: a single push to master containing
# a new component / parameter / enum auto-cascades to:
#   1. tools/lumeo-mcp/src/components-api.json    (this workflow)
#   2. tools/lumeo-mcp/src/registry.json           (this workflow)
#   3. src/Lumeo/registry/registry.json            (this workflow)
#   4. skills/lumeo/references/catalog.md          (sync-skill-catalog.yml)
#
# Without this, MCP/skill consumers drift behind library releases until
# someone remembers to regen manually — the same bug class that caused
# v2.0.1 (rc.38-shipped-under-2.0.0) and that 2.2.0 plugs structurally.

on:
  push:
    branches: [master]
    paths:
      - 'src/Lumeo/UI/**/*.razor'
      - 'src/Lumeo/UI/**/*.cs'
      - 'src/Lumeo.Charts/UI/**/*.razor'
      - 'src/Lumeo.Charts/UI/**/*.cs'
      - 'src/Lumeo.DataGrid/UI/**/*.razor'
      - 'src/Lumeo.DataGrid/UI/**/*.cs'
      - 'src/Lumeo.Editor/UI/**/*.razor'
      - 'src/Lumeo.Editor/UI/**/*.cs'
      - 'src/Lumeo.Scheduler/UI/**/*.razor'
      - 'src/Lumeo.Scheduler/UI/**/*.cs'
      - 'src/Lumeo.Gantt/UI/**/*.razor'
      - 'src/Lumeo.Gantt/UI/**/*.cs'
      - 'src/Lumeo.Motion/UI/**/*.razor'
      - 'src/Lumeo.Motion/UI/**/*.cs'
      - 'tools/Lumeo.RegistryGen/**/*.cs'
      - 'Directory.Build.props'
  workflow_dispatch:

permissions:
  contents: write

concurrency:
  group: sync-mcp-registry
  cancel-in-progress: false

jobs:
  regen:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          fetch-depth: 0

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '10.0.x'

      - name: Run RegistryGen
        run: dotnet run --project tools/Lumeo.RegistryGen

      - name: Commit + push if registry changed
        run: |
          # Both files RegistryGen writes — committed source of truth for
          # the MCP package and the in-repo lightweight component map.
          git add tools/lumeo-mcp/src/components-api.json \
                  tools/lumeo-mcp/src/registry.json \
                  src/Lumeo/registry/registry.json 2>/dev/null || true

          if git diff --cached --quiet; then
            echo "MCP registry already in sync with source — nothing to commit"
            exit 0
          fi

          # Commit author: Brain2k (project owner) to match the manual-commit
          # convention. github-actions[bot] would be fine too, but keeping
          # ownership consistent across automated commits and reviewers'
          # mental model.
          git config user.name  "Brain2k"
          git config user.email "53747224+Brain2k-0005@users.noreply.github.com"

          # Pull what RegistryGen calls "version" out of the file we just
          # wrote so the commit message names the exact version the catalog
          # corresponds to. Falls back to "unknown" if the JSON is malformed.
          version=$(grep -oP '"version":\s*"\K[^"]+' tools/lumeo-mcp/src/components-api.json | head -1 || echo "unknown")

          git commit -m "chore(mcp-sync): regenerate registry against current source ($version)

          Auto-generated by .github/workflows/sync-mcp-registry.yml whenever
          src/Lumeo*/UI/ or tools/Lumeo.RegistryGen/ changes on master.
          Keeps the MCP-facing catalog (tools/lumeo-mcp/src/components-api.json
          + registry.json) and the in-repo registry (src/Lumeo/registry/
          registry.json) in lockstep with the actual library API surface."

          git push origin master
