name: Preview

on:
  pull_request:

permissions:
  contents: write
  pull-requests: write

concurrency:
  group: preview-${{ github.event.pull_request.number }}
  cancel-in-progress: true

jobs:
  pkg-preview:
    name: Package Preview
    if: ${{ github.repository_owner == 'cloudflare' }}
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Dependencies
        uses: ./.github/actions/install-dependencies
        with:
          filter: "@cloudflare/kumo..."

      - name: Build @cloudflare/kumo
        run: pnpm --filter @cloudflare/kumo build

      - name: Publish preview package
        run: pnpm dlx pkg-pr-new publish --pnpm --compact --no-template ./packages/kumo

  # Stage 1: Build docs (runs for ALL PRs including forks).
  # Produces an artifact consumed by either docs-deploy (internal) or
  # the preview-deploy.yml workflow_run workflow (forks).
  docs-build:
    name: Docs Build
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Dependencies
        uses: ./.github/actions/install-dependencies
        with:
          filter: "@cloudflare/kumo-docs-astro..."

      - name: Build @cloudflare/kumo
        run: pnpm --filter @cloudflare/kumo build

      - name: Build docs
        run: pnpm --filter @cloudflare/kumo-docs-astro build

      - name: Upload docs artifact
        uses: actions/upload-artifact@v4
        with:
          name: docs-preview-dist
          path: packages/kumo-docs-astro/dist/
          retention-days: 1

  # Stage 2 (internal path): Deploy docs preview for non-fork PRs.
  # Runs in the same workflow with secrets available.
  # Fork PRs are handled by preview-deploy.yml (workflow_run).
  docs-deploy:
    name: Docs Deploy
    needs: docs-build
    if: ${{ github.event.pull_request.head.repo.full_name == github.repository }}
    runs-on: ubuntu-latest
    timeout-minutes: 10
    outputs:
      preview_url: ${{ steps.deploy.outputs.preview_url }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          sparse-checkout: packages/kumo-docs-astro/wrangler.jsonc

      - name: Download docs artifact
        uses: actions/download-artifact@v4
        with:
          name: docs-preview-dist
          path: packages/kumo-docs-astro/dist/

      - name: Install wrangler
        run: npm install -g wrangler

      - name: Deploy docs preview
        id: deploy
        working-directory: packages/kumo-docs-astro
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
        run: |
          COMMIT_SHORT="${GITHUB_SHA:0:7}"

          # "wrangler versions upload" creates a preview version without
          # routing production traffic. Safe to run on every PR.
          VERSION_OUTPUT=$(wrangler versions upload --env="" --message "PR #${PR_NUMBER} (${COMMIT_SHORT})" 2>&1) || true
          echo "$VERSION_OUTPUT"

          # Verify upload succeeded
          if ! echo "$VERSION_OUTPUT" | grep -q "Worker Version ID:"; then
            echo "::error::wrangler versions upload failed -- no Worker Version ID in output"
            exit 1
          fi

          # Extract preview URL from wrangler output
          PREVIEW_URL=$(echo "$VERSION_OUTPUT" | grep -oE 'Version Preview URL: https://[^ ]+' | sed 's/Version Preview URL: //')

          # Fallback: construct URL from version ID
          if [ -z "$PREVIEW_URL" ]; then
            VERSION_ID=$(echo "$VERSION_OUTPUT" | grep -oE 'Worker Version ID: [a-f0-9-]+' | sed 's/Worker Version ID: //')
            if [ -n "$VERSION_ID" ]; then
              VERSION_PREFIX=$(echo "$VERSION_ID" | cut -c1-8)
              PREVIEW_URL="https://${VERSION_PREFIX}-kumo-docs.design-engineering.workers.dev"
            fi
          fi

          if [ -z "$PREVIEW_URL" ]; then
            echo "::error::Failed to determine docs preview URL"
            exit 1
          fi

          echo "preview_url=$PREVIEW_URL" >> "$GITHUB_OUTPUT"

      - name: Comment on PR
        uses: actions/github-script@v7
        env:
          PREVIEW_URL: ${{ steps.deploy.outputs.preview_url }}
        with:
          script: |
            const marker = '<!-- kumo-docs-preview -->';
            const previewUrl = process.env.PREVIEW_URL;
            const body = [
              marker,
              '### Docs Preview',
              '',
              `[View docs preview](${previewUrl})`,
              '',
              `Commit: \`${context.payload.pull_request.head.sha.substring(0, 7)}\``,
            ].join('\n');

            const { data: comments } = await github.rest.issues.listComments({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.issue.number,
            });

            const existing = comments.find(c => c.body?.startsWith(marker));

            if (existing) {
              await github.rest.issues.updateComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                comment_id: existing.id,
                body,
              });
            } else {
              await github.rest.issues.createComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: context.issue.number,
                body,
              });
            }

  visual-regression:
    name: Visual Regression
    needs: docs-deploy
    if: ${{ github.event.pull_request.head.repo.full_name == github.repository && needs.docs-deploy.outputs.preview_url }}
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install Dependencies
        uses: ./.github/actions/install-dependencies
        with:
          filter: "@cloudflare/kumo..."

      - name: Run Visual Regression
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }}
          GITHUB_REPOSITORY: ${{ github.repository }}
          BEFORE_URL: "https://kumo-ui.com"
          AFTER_URL: ${{ needs.docs-deploy.outputs.preview_url }}
          SCREENSHOT_WORKER_URL: ${{ secrets.SCREENSHOT_WORKER_URL }}
          SCREENSHOT_API_KEY: ${{ secrets.SCREENSHOT_API_KEY }}
        run: pnpm tsx ci/visual-regression/run-visual-regression.ts

      - name: Upload Screenshots
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: visual-regression-screenshots
          path: ci/visual-regression/screenshots/
          retention-days: 7
