name: Update Demo VHS

on:
  workflow_dispatch:
  release:
    types: [published]

permissions:
  contents: write
  pull-requests: write

jobs:
  update-demo:
    name: Generate and Update Demo GIF
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version: "stable"

      - name: Build Matcha
        # We build the binary so we can run it inside the VHS container
        # (which likely doesn't have Go installed).
        run: go build -o matcha .

      - name: Create Dummy Config
        # Create the config in the workspace so we can map HOME to it for the container.
        run: |
          mkdir -p .config/matcha
          cat <<EOF > .config/matcha/config.json
          {
            "accounts": [
              {
                "id": "demo-user",
                "name": "Matcha Client",
                "email": "matcha@floatpane.com",
                "password": "dummy-password",
                "service_provider": "custom"
              }
            ]
          }
          EOF

      - name: Prepare Tape
        # Patch the tape to run the compiled binary instead of "go run ."
        # This works seamlessly because the typing of this command is 'Hidden' in your tape.
        run: sed -i 's/go run ./.\/matcha/' demo.tape

      - name: Run VHS
        uses: charmbracelet/vhs-action@v2
        with:
          path: "demo.tape"
          install-fonts: true
        env:
          # Point HOME to /github/workspace so the app finds .config/matcha/config.json
          HOME: /github/workspace

      - name: Move GIF to Assets
        run: sudo mv demo.gif public/assets/demo.gif

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v6
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: "docs: update demo.gif from release [skip ci]"
          title: "docs: update demo.gif"
          body: |
            This PR updates the demo GIF based on the latest release version.
            Generated automatically by the Update Demo VHS workflow.
          branch: "update-demo-gif"
          base: "main"
          add-paths: public/assets/demo.gif
