<%

  var st = !!locals.showStaff;
  var monthName = since.toLocaleDateString('en-GB', { month: 'long' });
  var KL = { branded: 'Branded', 'non-branded': 'Non-branded', unknown: 'No prompt data' };
  var tagOf = function (r) {
    var t = [sum.kindOf(r)];
    if (r.preExisting) t.push('pre'); else if (r.firstDetectedAt && new Date(r.firstDetectedAt) >= since) t.push('new');
    if (r.status === 'lost') t.push('lost');
    return t.join(' ');
  };
  var n = function (k) { return rows.filter(function (r) { return (' ' + tagOf(r) + ' ').indexOf(' ' + k + ' ') > -1; }).length; };
  var CH = [['', 'All', rows.length], ['new', 'New this month', n('new')], ['non-branded', 'Non-branded', n('non-branded')], ['branded', 'Branded', n('branded')], ['unknown', 'No prompt data', n('unknown')], ['pre', 'Cited before our link', n('pre')], ['lost', 'Not cited now', n('lost')]];
  var engines = Object.keys(sum.engines).sort(function (a, b) { return sum.engines[b] - sum.engines[a]; });
%>
<%- include('../partials/lb-shell') %>

<p class="claim-lede"><% if (!rows.length) { %>No AI citations of our articles yet.<% } else { %><em><%= sum.fresh ? '+' + sum.fresh + ' new' : 'No new' %></em> article<%= sum.fresh === 1 ? '' : 's' %> cited by AI in <%= monthName %> · <%= sum.unique %> unique article<%= sum.unique === 1 ? '' : 's' %> cited now (<%= sum.hits %> hit<%= sum.hits === 1 ? '' : 's' %> across AI sources).<% } %></p>
<p class="claim-sub">An article counts when ChatGPT, Gemini, Perplexity, Claude or Google's AI cite the page our link sits on, in their answers to the <%= tracked %> question<%= tracked === 1 ? '' : 's' %> we track for <%= brand %>.</p>

<div class="bento" style="margin-top:18px">
  <section class="card b12 rp-sec">
    <div class="rp-stats">
      <div class="rp-stat"><div class="k">Unique articles cited</div><div class="v"><%= sum.unique %></div><div class="s">of <%= liveLinks %> live link<%= liveLinks === 1 ? '' : 's' %></div></div>
      <div class="rp-stat"><div class="k">Citation hits</div><div class="v"><%= sum.hits %></div><div class="s">one engine, one question</div></div>
      <div class="rp-stat"><div class="k">New in <%= monthName %></div><div class="v"><%= sum.fresh ? '+' + sum.fresh : 0 %></div><div class="s"><%= sum.freshBy['non-branded'] %> non-branded · <%= sum.freshBy.branded %> branded<%= sum.freshBy.unknown ? ' · ' + sum.freshBy.unknown + ' no prompt' : '' %></div></div>
      <div class="rp-stat"><div class="k">Not cited now</div><div class="v"><%= sum.lost %></div><div class="s">absent from the latest answers</div></div>
    </div>
    <% if (engines.length) { %><div class="rp-body"><div class="rp-filters" style="font-size:12px"><span class="rr-meta">Hits by source</span><% engines.forEach(function (e) { %><span class="rp-lab plain" style="font-weight:500; display:inline-flex; gap:6px; align-items:center"><%- include('../partials/engine-mark', { engine: e, size: 'sm' }) %><%= e %> <b><%= sum.engines[e] %></b></span><% }) %></div></div><% } %>
  </section>

  <section class="card b12 rp-sec rep-fbox">
    <div class="rp-sh"><div><h3>Cited articles</h3></div>
      <div class="rp-filters rep-f"><% CH.forEach(function (c, i) { if (i && !c[2]) return; %><button type="button" class="rp-chip<%= i ? '' : ' on' %>" data-f="<%= c[0] %>" style="--c:var(--<%= c[0] === 'new' ? 'accent-strong' : c[0] === 'lost' ? 'neg' : 'muted-2' %>)"><% if (i) { %><i></i><% } %><%= c[1] %> <b><%= c[2] %></b></button><% }) %></div>
    </div>
    <% if (!rows.length) { %><div class="rp-empty"><b>Nothing detected yet</b><%= liveLinks ? 'We look every day, from the AI answers we already collect. The first citation shows up here, and the team hears about it in HRMS.' : 'Citations are looked for once there are live links.' %></div><% } else { %>
    <ul class="rp-list">
      <% rows.forEach(function (r) { var o = firstObs(r) || {}; var l = linkOf.get(String(r.backlink)) || {}; var gp = l.guestPost && gpOf.get(String(l.guestPost)); var engs = Array.from(new Set((r.obs || []).map(function (x) { return x.engine; }))); var t = tagOf(r); var others = (r.obs || []).filter(function (x) { return x !== o; }); %>
        <li class="rp-item<%= r.status === 'lost' ? ' is-done' : '' %>" data-k="<%= t %>" id="c-<%= r._id %>">
          <div class="rp-lead" style="padding-top:3px"><img src="https://www.google.com/s2/favicons?domain=<%= r.domain %>&sz=32" alt="" width="18" height="18"></div>
          <div>
            <div class="rp-src"><b style="color:var(--ink)"><%= r.domain %></b><% if (/ new/.test(' ' + t)) { %><span class="lb-new">NEW</span><% } %><% if (r.preExisting) { %><span class="lb-pre" title="The engine cited this article before our link was placed in it">Cited before our link</span><% } %><span>— cited by <%= engs.join(', ') %></span></div>
            <% if (gp && gp.title) { %><div class="rp-t"><%= gp.title %></div><% } %>
            <a class="lb-url" href="<%= r.articleUrl %>" target="_blank" rel="noopener nofollow"><%= r.articleUrl %></a>
            <dl class="lb-kv" style="margin-top:10px; grid-template-columns:110px minmax(0, 1fr)">
              <dt>Prompt</dt><dd <%- h.dirAttr(o.prompt) %>><%= o.prompt ? '“' + o.prompt + '”' : '—' %><%= o.lang ? ' · ' + o.lang : '' %> · <span class="rr-meta"><%= KL[o.branded || 'unknown'] %></span></dd>
              <dt>Placed</dt><dd><%= lb.dmy(l.placedAt) %><%= l.placedBy ? ' · by ' + l.placedBy : '' %></dd>
              <dt>First cited</dt><dd><%= lb.dmy(r.firstDetectedAt) %><%= l.placedAt ? ' (' + lb.gap(l.placedAt, r.firstDetectedAt) + ')' : '' %></dd>
            </dl>
            <% if (others.length) { %><details class="rp-det"><summary><%= others.length %> more citation<%= others.length === 1 ? '' : 's' %> of this article</summary>
              <ul class="rep-log"><% others.forEach(function (x) { %><li><b><%= x.engine %></b> <span <%- h.dirAttr(x.prompt) %>><%= x.prompt ? '“' + x.prompt + '”' : 'no prompt data' %></span><%= x.lang ? ' · ' + x.lang : '' %> <span class="rr-meta">· since <%= lb.dmy(x.firstAt) %><%= x.active ? '' : ' · not now' %></span></li><% }) %></ul></details><% } %>
          </div>
          <div class="rp-side">
            <span class="rp-lab sc-<%= r.status === 'active' ? 'approved' : 'dismissed' %>"><%= r.status === 'active' ? 'Cited now' : 'Not cited now' %></span>
            <a class="btn btn-sm" href="/link-building/links/<%= r.backlink %>">The link</a>
          </div>
        </li>
      <% }) %>
    </ul>
    <% } %>
    <div class="rp-note"><b>How to read this.</b> <b>First cited</b> is the first time we saw the citation, not necessarily the first time the engine made it. <b>Cited before our link</b> means the engine already cited the article before our link was placed in it: real, but not something the link earned, so it is never counted as new. <b>Not cited now</b> means it is missing from the latest answers we hold; AI answers vary, so this is not proof it is gone for good. We only see answers to the questions tracked for <%= brand %><%= st ? ' (add more on Prompt Explorer or Reputation → Settings)' : '' %>.</div>
  </section>
</div>
<%- include('../partials/rep-foot') %>
