---
name: 'PR: Vercel Website Preview'

on:
  workflow_run:
    workflows: ['CI: Vercel Website Preview']
    types:
      - completed

concurrency:
  group: ${{ github.workflow }}-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }}
  cancel-in-progress: true

jobs:
  comment:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
      actions: read
    # Uses head_branch as proxy for PR number (unavailable at job-level in workflow_run).
    # Preview and E2E comment writes are NOT mutually serialized — the race window is
    # small and self-healing on next push.
    concurrency:
      group: website-pr-comment-${{ github.event.workflow_run.head_branch }}
      cancel-in-progress: false
    if: >
      github.repository == 'Comfy-Org/ComfyUI_frontend' &&
      github.event.workflow_run.event == 'pull_request' &&
      github.event.workflow_run.conclusion == 'success'
    steps:
      - uses: actions/checkout@v6

      - name: Download preview metadata
        uses: dawidd6/action-download-artifact@0bd50d53a6d7fb5cb921e607957e9cc12b4ce392 # v12
        with:
          name: vercel-preview
          run_id: ${{ github.event.workflow_run.id }}
          path: temp/vercel-preview

      - name: Resolve PR number from workflow_run context
        id: pr-meta
        uses: ./.github/actions/resolve-pr-from-workflow-run

      - name: Read preview URLs
        if: steps.pr-meta.outputs.skip != 'true'
        id: urls
        run: |
          echo "stable-url=$(cat temp/vercel-preview/stable-url.txt)" >> "$GITHUB_OUTPUT"
          echo "unique-url=$(cat temp/vercel-preview/url.txt)" >> "$GITHUB_OUTPUT"
          echo "short-sha=${HEAD_SHA:0:7}" >> "$GITHUB_OUTPUT"
        env:
          HEAD_SHA: ${{ github.event.workflow_run.head_sha }}

      - name: Post preview comment
        if: steps.pr-meta.outputs.skip != 'true'
        uses: ./.github/actions/upsert-comment-section
        with:
          pr-number: ${{ steps.pr-meta.outputs.number }}
          section-name: preview
          comment-marker: '<!-- WEBSITE_CI_REPORT -->'
          token: ${{ secrets.GITHUB_TOKEN }}
          section-content: |-
            ## 🔗 Website Preview

            **Website Preview:** ${{ steps.urls.outputs.stable-url }}

            <sub>This commit: ${{ steps.urls.outputs.unique-url }}</sub>

            <sub>Last updated: ${{ github.event.workflow_run.updated_at }} for `${{ steps.urls.outputs.short-sha }}`</sub>
