name: Auto Format

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

permissions:
  contents: write

concurrency:
  group: auto-format-${{ github.head_ref || github.ref_name }}
  cancel-in-progress: true

jobs:
  format:
    name: Auto Format
    runs-on: ubuntu-latest
    # Skip if:
    # - PR from fork (can't push back)
    # - Commit by github-actions[bot] (prevent infinite loop)
    if: |
      (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) &&
      github.actor != 'github-actions[bot]'
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.head_ref || github.ref_name }}
          path: meta_plugin_protocol
          # Use PAT to trigger downstream workflows; fallback to GITHUB_TOKEN
          token: ${{ secrets.PARENT_REPO_PAT || github.token }}

      - name: Create workspace Cargo.toml
        run: |
          cat > Cargo.toml << 'EOF'
          [workspace]
          members = ["meta_plugin_protocol"]
          resolver = "2"

          [workspace.package]
          version = "0.1.0"
          edition = "2021"
          license = "MIT"
          repository = "https://github.com/gitkb/meta"
          EOF

      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt

      - name: Run cargo fmt
        working-directory: meta_plugin_protocol
        run: cargo fmt --all

      - name: Check for changes
        id: changes
        working-directory: meta_plugin_protocol
        run: |
          if git diff --quiet; then
            echo "formatted=false" >> $GITHUB_OUTPUT
          else
            echo "formatted=true" >> $GITHUB_OUTPUT
          fi

      - name: Commit and push
        if: steps.changes.outputs.formatted == 'true'
        working-directory: meta_plugin_protocol
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add -A
          git commit -m "style: auto-format code"
          git push
