<%

  var st = !!locals.showStaff;
  var monthName = since.toLocaleDateString('en-GB', { month: 'long' });
  var q = quota;
  var qPct = q.of ? Math.min(100, Math.round(q.done / q.of * 100)) : 0;
  var STC = { live: 'var(--pos)', recovered: '#3A86C8', review: '#E2A03F', pending: '#9AA3AE', lost: 'var(--neg)' };
  var bar = ['live', 'recovered', 'review', 'pending', 'lost'].map(function (k) { return [k, m[k] != null ? m[k] : 0]; });
  bar[0][1] = m.live - m.review - m.recovered;
  var allLinks = bar.reduce(function (n, x) { return n + x[1]; }, 0);
%>
<%- include('../partials/lb-shell') %>

<% if (!m.total && !m.inPipeline) { %>
  <p class="claim-lede">No links yet for <%= brand %>.</p>
  <p class="claim-sub"><%= st ? 'Add the client\'s target pages on Link Plan, propose sites on Sites, then start a guest post.' : 'Your VOCTOS team adds every guest post and link here as the work starts.' %></p>
<% } else { %>
  <p class="claim-lede"><% if (q.of) { %><em><%= q.done %> of <%= q.of %> links</em> verified in <%= monthName %><% } else { %><em><%= q.done %> link<%= q.done === 1 ? '' : 's' %></em> verified in <%= monthName %><% } %>, <%= m.live %> live across <%= m.domains %> site<%= m.domains === 1 ? '' : 's' %><%= sum.unique ? ', and ' + sum.unique + ' of our articles cited by AI' : '' %>.</p>
  <p class="claim-sub">A link counts once it passes its first check on the live page. Each one is checked again every <%= cfg.checkEveryDays || 14 %> days, and the next day when something looks wrong.</p>
<% } %>

<div class="bento" style="margin-top:18px">
  <% if (waitingPosts.length || waitingSites.length) { %>
  <section class="card b12 rp-sec">
    <div class="rp-sh"><div><div class="rp-kick"><%= st ? 'With the client' : 'You' %></div><h3><%= waitingPosts.length + waitingSites.length %> waiting for approval</h3></div></div>
    <ul class="rp-list">
      <% waitingPosts.forEach(function (p) { var _u = p._content ? '/link-building/content/' + p._id : '/link-building/posts/' + p._id; %><li class="rp-item"><div class="rp-lead">✎</div><div><div class="rp-src"><%= p._content ? 'Article' : 'Article for ' + p.domain %> · version <%= lb.latest(p) %></div><a class="rp-t" href="<%= _u %>"><%= p.title || 'Untitled' %></a></div><div class="rp-side"><a class="btn btn-sm<%= st ? '' : ' btn-primary' %>" href="<%= _u %>"><%= st ? 'Open' : 'Read and approve' %></a></div></li><% }) %>
      <% waitingSites.forEach(function (s) { %><li class="rp-item"><div class="rp-lead"><img src="https://www.google.com/s2/favicons?domain=<%= s.domain %>&sz=32" alt="" width="18" height="18"></div><div><div class="rp-src">Site to approve</div><div class="rp-t"><%= s.domain %></div><div class="rp-why"><%= [s.dr != null ? 'DR ' + s.dr : '', s.traffic != null ? h.int(s.traffic) + ' visits/mo' : '', s.niche || ''].filter(Boolean).join(' · ') %></div></div><div class="rp-side"><a class="btn btn-sm<%= st ? '' : ' btn-primary' %>" href="/link-building/sites#s-<%= s._id %>"><%= st ? 'Open' : 'Decide' %></a></div></li><% }) %>
    </ul>
  </section>
  <% } %>

  <section class="card b12 rp-sec">
    <div class="rp-stats">
      <div class="rp-stat"><div class="k">Links verified in <%= monthName %></div><div class="v"><%= q.done %><% if (q.of) { %><span style="font-size:15px; color:var(--muted); font-weight:600"> / <%= q.of %></span><% } %></div><% if (q.of) { %><div class="lb-meter"><i style="width:<%= qPct %>%"></i></div><% } else { %><div class="s">No monthly quota set</div><% } %></div>
      <div class="rp-stat"><div class="k">Referring domains</div><div class="v"><%= m.domains %></div><div class="s"><%= m.live %> live link<%= m.live === 1 ? '' : 's' %> in all</div></div>
      <div class="rp-stat"><div class="k">Articles cited by AI</div><div class="v"><%= sum.unique %></div><div class="s"><%= sum.hits %> hit<%= sum.hits === 1 ? '' : 's' %> across AI answers</div></div>
      <div class="rp-stat"><div class="k">New AI citations</div><div class="v"><%= sum.fresh ? '+' + sum.fresh : '0' %></div><div class="s">since 1 <%= monthName %></div></div>
      <div class="rp-stat<%= m.lost ? ' neg' : '' %>"><div class="k"><%= st ? 'Need a look' : 'Lost links' %></div><div class="v"><%= st ? m.review + m.lost : m.lost %></div><div class="s"><%= st ? m.lost + ' lost, ' + m.review + ' to review' : (st || !m.lost ? 'none right now' : 'we replace or restore these') %></div></div>
    </div>
    <% if (allLinks) { %>
    <div class="rp-body">
      <div class="lb-bar" title="Link status"><% bar.forEach(function (x) { if (!x[1]) return; %><i style="width:<%= x[1] / allLinks * 100 %>%; background:<%= STC[x[0]] %>" title="<%= lb.STATUS[x[0]] %>: <%= x[1] %>"></i><% }) %></div>
      <div class="rp-filters" style="margin-top:10px; font-size:12px"><% bar.forEach(function (x) { if (!x[1]) return; %><span class="rp-lab" style="--c:<%= STC[x[0]] %>; font-weight:500"><%= lb.STATUS[x[0]] %> <b><%= x[1] %></b></span><% }) %></div>
    </div>
    <% } %>
  </section>

  <section class="card b7 rp-sec">
    <div class="rp-sh"><div><h3>Recent activity</h3></div><div class="rp-sm"><a href="/link-building/links">Live links →</a></div></div>
    <% if (!feed.length) { %><div class="rp-empty"><b>Nothing yet</b>Every approval, check and citation shows up here.</div><% } else { %>
    <ul class="lb-feed">
      <% feed.forEach(function (f) { %><li class="<%= f.kind %>"><time><%= h.date(f.at) %></time><span><a href="<%= f.url %>"><b><%= f.what %></b></a> · <%= f.on %></span></li><% }) %>
    </ul>
    <% } %>
  </section>

  <section class="card b5 rp-sec">
    <div class="rp-sh"><div><div class="rp-kick">AI Citations</div><h3>Latest cited articles</h3></div><div class="rp-sm"><a href="/link-building/citations">All →</a></div></div>
    <% if (!cites.length) { %><div class="rp-empty"><b>None detected yet</b>When ChatGPT, Gemini, Perplexity or Google AI cite one of our articles, it appears here.</div><% } else { %>
    <ul class="rp-list">
      <% cites.slice(0, 5).forEach(function (c) { var engines = Array.from(new Set((c.obs || []).map(function (o) { return o.engine; }))); %>
        <li class="rp-item" style="grid-template-columns:24px minmax(0, 1fr)"><div class="rp-lead" style="padding-top:2px"><img src="https://www.google.com/s2/favicons?domain=<%= c.domain %>&sz=32" alt="" width="16" height="16"></div>
          <div><div class="rp-src"><b style="color:var(--ink)"><%= c.domain %></b><% if (c.preExisting) { %><span class="lb-pre">Cited before our link</span><% } else if (c.firstDetectedAt >= since) { %><span class="lb-new">NEW</span><% } %><% if (c.status === 'lost') { %><span class="rp-lab sc-dismissed">Not cited now</span><% } %></div>
          <div class="rp-why"><%= engines.join(', ') %> · first seen <%= lb.dmy(c.firstDetectedAt) %></div></div></li>
      <% }) %>
    </ul>
    <% } %>
  </section>
</div>
<%- include('../partials/rep-foot') %>
