name: Update token count

on:
  workflow_dispatch:
  push:
    branches: [main]
    paths: ['src/**', 'container/**', 'launchd/**', 'CLAUDE.md']

jobs:
  update-tokens:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/create-github-app-token@v1
        id: app-token
        with:
          app-id: ${{ secrets.APP_ID }}
          private-key: ${{ secrets.APP_PRIVATE_KEY }}

      - uses: actions/checkout@v4
        with:
          token: ${{ steps.app-token.outputs.token }}

      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - uses: ./repo-tokens
        id: tokens
        with:
          include: 'src/**/*.ts container/agent-runner/src/**/*.ts container/Dockerfile container/build.sh launchd/com.nanoclaw.plist CLAUDE.md'
          exclude: 'src/**/*.test.ts'
          badge-path: 'repo-tokens/badge.svg'

      - name: Commit if changed
        run: |
          git add README.md repo-tokens/badge.svg
          git diff --cached --quiet && exit 0
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git commit -m "docs: update token count to ${{ steps.tokens.outputs.badge }}"
          git pull --rebase
          git push
