name: Sync changes to **/ai-personas.json

on:
  push:
    branches: [main]
    paths: ["**/ai-personas.json"]

jobs:
  build:
    if: (github.repository == 'KudoAI/ai-personas') && !contains(github.event.head_commit.message, '[auto-sync')
    runs-on: ubuntu-24.04
    permissions:
      contents: read
    env:
      TZ: PST8PDT

    steps:

      - name: Checkout KudoAI/ai-personas
        uses: actions/checkout@v6.0.2
        with:
          token: ${{ secrets.REPO_SYNC_PAT }}
          path: KudoAI/ai-personas
          fetch-depth: 2

      - name: Checkout adamlui/js-utils
        uses: actions/checkout@v6.0.2
        with:
          token: ${{ secrets.REPO_SYNC_PAT }}
          repository: adamlui/js-utils
          path: adamlui/js-utils

      - name: Checkout adamlui/python-utils
        uses: actions/checkout@v6.0.2
        with:
          token: ${{ secrets.REPO_SYNC_PAT }}
          repository: adamlui/python-utils
          path: adamlui/python-utils

      - name: Sync ai-personas.json across repo
        run: |
          cd ${{ github.workspace }}/KudoAI/ai-personas

          # Find the newest ai-personas.json
          files=$(find . -name "ai-personas.json" -type f)
          newest_file="" ; newest_timestamp=0
          for file in $files ; do
            timestamp=$(git log -1 --format="%ct" -- "$file")
            if (( timestamp > newest_timestamp )) ; then
              newest_file=$file
              newest_timestamp=$timestamp
            fi
          done

          # Copy/paste over all other ai-personas.json files
          if [[ -n "$newest_file" ]] ; then
            for file in $files ; do
              if [[ "$file" != "$newest_file" ]] ; then
                cp -f "$newest_file" "$file" ; fi
            done
          fi

          echo "SYNC_SRC=$newest_file" >> $GITHUB_ENV

      - name: Sync node.js/ → adamlui/js-utils/ai-personas/
        run: |
          rsync -avhc --delete \
            ${{ github.workspace }}/KudoAI/ai-personas/node.js/ \
            ${{ github.workspace }}/adamlui/js-utils/ai-personas/

      - name: Sync python/ → adamlui/python-utils/ai-personas/
        run: |
          rsync -avhc --delete \
            ${{ github.workspace }}/KudoAI/ai-personas/python/ \
            ${{ github.workspace }}/adamlui/python-utils/ai-personas/

      - name: Escape backticks in commit msg
        env:
          COMMIT_MSG: ${{ github.event.head_commit.message }}
        run: |
          DELIM="EOF_$(uuidgen)"
          echo "ESCAPED_MSG<<$DELIM" >> "$GITHUB_ENV"
          echo "$COMMIT_MSG" | sed 's/`/\`/g' >> "$GITHUB_ENV"
          echo "$DELIM" >> "$GITHUB_ENV"

      - name: Config committer
        env:
          GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
          GPG_PRIVATE_ID: ${{ secrets.GPG_PRIVATE_ID }}
        run: |
          gpg --batch --import <(echo "$GPG_PRIVATE_KEY")
          git config --global commit.gpgsign true
          git config --global user.name "kudo-sync-bot"
          git config --global user.email "auto-sync@kudoai.com"
          git config --global user.signingkey "$GPG_PRIVATE_ID"

      - name: Push changes to KudoAI/ai-personas
        if: env.SYNC_SRC
        run: |
          cd ${{ github.workspace }}/KudoAI/ai-personas
          git add . && git commit -n -m "$ESCAPED_MSG ↞ [auto-sync from $SYNC_SRC]" || true
          git pull --rebase
          git push

      - name: Push changes to js-utils
        if: env.SYNC_SRC
        run: |
          cd ${{ github.workspace }}/adamlui/js-utils
          git add . && git commit -n -m \
            "$ESCAPED_MSG ↞ [auto-sync from https://github.com/KudoAI/ai-personas/tree/main/node.js]" || true
          git pull --rebase
          git push

      - name: Push changes to python-utils
        if: env.SYNC_SRC
        run: |
          cd ${{ github.workspace }}/adamlui/python-utils
          git add . && git commit -n -m \
            "$ESCAPED_MSG ↞ [auto-sync from https://github.com/KudoAI/ai-personas/tree/main/python]" || true
          git pull --rebase
          git push
