name: Update Screenshots

on:
  push:
    branches: [main]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: false

permissions: {}

jobs:
  screenshots:
    name: Generate Screenshots
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd  # v6
        with:
          persist-credentials: false

      - uses: dtolnay/rust-toolchain@29eef336d9b2848a0b548edc03f92a220660cdb8  # stable
      - uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32  # v2
        with:
          shared-key: cargo-${{ runner.os }}

      - name: Install termshot
        run: |
          curl --silent --location \
            "https://raw.githubusercontent.com/homeport/termshot/main/hack/download.sh" \
            | bash

      - name: Build release binary
        run: cargo build --release

      - name: Generate screenshots
        run: bash scripts/demo-screenshots.sh

      - name: Commit updated screenshots
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git diff --quiet assets/demo/ && exit 0
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
          git add assets/demo/
          git commit -m "chore: update demo screenshots [skip ci]"
          git pull --rebase
          git push
