name: Auto Format

on:
  pull_request:
    types: [opened, synchronize]

permissions:
  contents: write

jobs:
  format:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '24'

      - name: Run Biome format
        # Pin to the version in package.json so CI matches local/pre-commit
        # (npx @latest drifts — e.g. 2.5.0 broke this job on unrelated PRs).
        run: npx @biomejs/biome@2.4.13 check --write --no-errors-on-unmatched .

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

      # For fork PRs, just fail if formatting is needed (can't push to forks)
      - name: Fail if fork PR needs formatting
        if: steps.changes.outputs.has_changes == 'true' && github.event.pull_request.head.repo.full_name != github.repository
        run: |
          echo "::error::This PR has formatting issues. Please run 'npx @biomejs/biome check --write .' locally and push the changes."
          git diff --stat
          exit 1

      # For same-repo PRs, commit and push the changes
      - name: Commit changes
        if: steps.changes.outputs.has_changes == 'true' && github.event.pull_request.head.repo.full_name == github.repository
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
          git add .
          git commit -m "style: auto-format with Biome"
          git push origin HEAD:${{ github.head_ref }}
