name: PR Health rollup

on:
  pull_request:
    branches: [main]
    types: [opened, synchronize, reopened, labeled, unlabeled]
  workflow_run:
    workflows: [CI, Preview smoke, Visual diff]
    types: [completed]

permissions:
  pull-requests: write
  issues: write
  checks: read

jobs:
  rollup:
    name: Aggregate gate status
    runs-on: ubuntu-latest
    steps:
      - name: Compute status + post sticky comment
        uses: actions/github-script@v7
        with:
          script: |
            const { owner, repo } = context.repo;
            const pr_number = context.payload.pull_request?.number
              ?? context.payload.workflow_run?.pull_requests?.[0]?.number;

            if (!pr_number) {
              core.info('No PR context — skipping rollup.');
              return;
            }

            const pr = (await github.rest.pulls.get({ owner, repo, pull_number: pr_number })).data;
            const sha = pr.head.sha;

            const checks = (await github.rest.checks.listForRef({ owner, repo, ref: sha, per_page: 100 })).data.check_runs;
            const find = (name) => checks.find(c => c.name === name);

            const skip = (flag) =>
              new RegExp(`pipeline:.*skip[^\\n]*\\b${flag}\\b`).test(pr.body || '');

            const row = (label, run, opt = false) => {
              if (!run) return `| ${label} | ${opt ? '⏭ skipped or pending' : '⏳ pending'} |`;
              if (run.status !== 'completed') return `| ${label} | ⏳ in progress |`;
              const ok = run.conclusion === 'success';
              return `| ${label} | ${ok ? '✅ pass' : '❌ ' + run.conclusion} |`;
            };

            const rows = [
              row('CI: Lint, types, build', find('Lint, types, build')),
              // CI: Tests row omitted — echos-ocr has no test runner yet.
              // Re-enable once a `test:run` script lands in package.json and
              // the `test:` job in ci.yml is uncommented.
              row('CI: Schema map fresh', find('Schema map up to date')),
              skip('smoke') ? '| Preview smoke | ⏭ skipped (pipeline directive) |' : row('Preview smoke', find('Playwright smoke'), true),
              skip('visual') ? '| Visual diff | ⏭ skipped (pipeline directive) |' : row('Visual diff', find('Screenshot diff'), true),
            ];

            const reviewer_comment = (await github.rest.issues.listComments({
              owner, repo, issue_number: pr_number, per_page: 100,
            })).data.find(c => c.body?.startsWith('## Reviewer Report'));

            const a11y_comment = (await github.rest.issues.listComments({
              owner, repo, issue_number: pr_number, per_page: 100,
            })).data.find(c => c.body?.startsWith('## A11y Audit'));

            const ds_comment = (await github.rest.issues.listComments({
              owner, repo, issue_number: pr_number, per_page: 100,
            })).data.find(c => c.body?.startsWith('## Design System Audit'));

            const role_row = (label, c, skipped) =>
              skipped ? `| ${label} | ⏭ skipped |` : c ? `| ${label} | ✅ posted |` : `| ${label} | ⏳ pending |`;

            const role_rows = [
              role_row('Reviewer report', reviewer_comment, skip('review')),
              role_row('A11y audit', a11y_comment, skip('a11y')),
              role_row('Design system audit', ds_comment, skip('design')),
            ];

            const marker = '<!-- pipeline-rollup -->';
            const body = `${marker}\n## Pipeline Health\n\n### CI gates\n\n| Gate | Status |\n| --- | --- |\n${rows.join('\n')}\n\n### Role reports\n\n| Role | Status |\n| --- | --- |\n${role_rows.join('\n')}\n\nSee individual comments above for details. This rollup updates automatically.`;

            const comments = (await github.rest.issues.listComments({
              owner, repo, issue_number: pr_number, per_page: 100,
            })).data;
            const existing = comments.find(c => c.body?.startsWith(marker));

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