name: Optimize SVGs

on:
  push:
    branches:
      - main
  workflow_dispatch:

permissions:
  contents: write

jobs:
  optimize-svg:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm

      - name: Optimize SVGs
        run: |
          npx --yes svgo@4 \
            --recursive \
            --folder . \
            --multipass \
            --final-newline

      - name: Commit optimized SVGs
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"

          git add -- '*.svg'

          if git diff --cached --quiet; then
            echo "No SVG optimization changes."
          else
            git commit -m "Optimize SVG assets"
            git push
          fi
