name: Backward Sync (Main to Dev)
on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  sync-to-dev:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout dev 🚀
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: dev
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Config Git 🛠️
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

      - name: Backport Main into Dev 🔙
        run: |
          # Merge main into dev
          # We don't commit immediately so we can fix Cargo.toml and restore dirs
          git merge origin/main --no-commit --no-ff || true
          
          # PROTECT THE VIBE 🛡️
          # Restore dev's version of everything that 'main' wants to delete
          git checkout HEAD -- ACE/ ACViewer/ docs/ ace-root/ crates/holtburger-debug-harness/ .gitmodules Cargo.toml
          
          # Stage fixes
          git add .
          
          # Commit the merge
          if git diff-index --quiet HEAD; then
            echo "Nothing to backport. Main and dev are synced. 🤝"
          else
            git commit -m "chore: backport from main (restoring dev-only folders) [skip ci]"
            git push origin dev
          fi
