name: Recording Evaluation

on:
  pull_request:
    types: [opened, reopened, synchronize, closed]
  workflow_dispatch:

concurrency:
  group: preview-${{ github.ref }}
  cancel-in-progress: true

jobs:
  evaluate:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    env:
      ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
    steps:
      - uses: actions/checkout@v4

      - name: Install system dependencies
        if: github.event.action != 'closed'
        run: |
          sudo apt-get update
          sudo apt-get install -y fonts-hack-ttf neovim
          fc-cache -fv

      - name: Setup Node.js
        if: github.event.action != 'closed'
        uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "npm"

      - name: Install Claude CLI
        if: github.event.action != 'closed'
        run: npm install -g @anthropic-ai/claude-code

      - name: Install shellwright dependencies
        if: github.event.action != 'closed'
        run: npm ci

      - name: Build shellwright
        if: github.event.action != 'closed'
        run: npm run build

      - name: Install evaluation dependencies
        if: github.event.action != 'closed'
        working-directory: evaluations
        run: npm install

      - name: Run evaluations
        if: github.event.action != 'closed'
        working-directory: evaluations
        run: npm start

      - name: Generate comparison
        if: github.event.action != 'closed'
        working-directory: evaluations
        env:
          GITHUB_REPOSITORY: ${{ github.repository }}
          GITHUB_HEAD_REF: ${{ github.head_ref }}
        run: npm run compare

      - name: Deploy recordings to GitHub Pages
        uses: rossjrw/pr-preview-action@v1
        id: preview
        with:
          source-dir: ./evaluations/scenarios/
          preview-branch: gh-pages
          umbrella-dir: pr-preview
          action: auto
          comment: false

      - name: Comment with preview link
        if: github.event.action != 'closed'
        uses: marocchino/sticky-pull-request-comment@v2
        with:
          header: recording-eval
          message: |
            ## Evidence
            ### Screenshots and Recordings
            **[View Recordings & Review](${{ steps.preview.outputs.preview-url }})**

      - name: Upload recordings
        if: github.event.action != 'closed'
        uses: actions/upload-artifact@v4
        with:
          name: recordings
          path: |
            evaluations/scenarios/*/*.gif
            evaluations/scenarios/*/*.png
          if-no-files-found: warn
