name: Update Demo VHS

on:
  workflow_dispatch:
  release:
    types: [published]

permissions:
  contents: write
  pull-requests: write

jobs:
  update-demo:
    if: github.event_name == 'workflow_dispatch' || github.event.release.prerelease == false
    name: Generate and Update Demo GIF
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          persist-credentials: false

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

      - name: Install system dependencies
        run: sudo apt-get update && sudo apt-get install -y libpcsclite-dev

      - 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",
                "fetch_email": "matcha@floatpane.com"
              }
            ]
          }
          EOF
      - name: Build Matcha
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          LATEST_TAG=$(gh release view --json tagName -q ".tagName")
          echo "Found latest release: $LATEST_TAG"

          git fetch --tags

          COMMIT=$(git rev-parse --short HEAD)
          DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
          go build -ldflags="-s -w -X main.version=$LATEST_TAG -X main.commit=$COMMIT -X main.date=$DATE" -o matcha .

      - 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'
        env:
          CLICOLOR_FORCE: "1"
        run: |
          sudo apt update
          sudo apt install -y ffmpeg ttyd
          mkdir -p ~/.local/share/fonts
          wget https://github.com/ryanoasis/nerd-fonts/releases/download/v3.1.1/JetBrainsMono.zip
          unzip -o JetBrainsMono.zip -d ~/.local/share/fonts
          fc-cache -fv
          go install github.com/charmbracelet/vhs@latest
          TERM=xterm-256color vhs demo.tape

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

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v8
        with:
          token: ${{ secrets.HOMEBREW_GITHUB_TOKEN }}
          commit-message: "docs: update demo.gif from release"
          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: "master"
          add-paths: public/assets/demo.gif
