<%

  var st = !!locals.showStaff;
  var s = l.seen || {};
%>
<%- include('../partials/lb-shell', { crumb: ['Live Links', '/link-building/links'] }) %>

<p class="claim-sub" style="margin-top:4px"><span class="rp-lab sc-<%= lb.STATUS_C[l.status] %>"><%= lb.STATUS[l.status] %></span> &nbsp;<a class="lb-url" href="<%= l.sourceUrl %>" target="_blank" rel="noopener"><%= l.sourceUrl %></a></p>

<div class="bento" style="margin-top:18px">
  <section class="card b7 rp-sec">
    <div class="rp-sh"><div><h3>The link</h3></div><% if (st && l.status !== 'replaced') { %><form class="js-rep" method="POST" action="/api/links/backlinks/<%= l._id %>/check"><button class="btn btn-primary btn-sm" type="submit">Check now</button></form><% } %></div>
    <div class="rp-body"><dl class="lb-kv">
      <dt>Points to</dt><dd><a href="<%= l.targetUrl %>" target="_blank" rel="noopener"><%= l.targetUrl %></a></dd>
      <dt>Anchor</dt><dd><%= s.found ? '“' + s.anchor + '”' : '—' %><% if (l.expect && l.expect.anchor && s.found && s.anchor.toLowerCase() !== l.expect.anchor.toLowerCase()) { %> <span class="rr-meta">(agreed: “<%= l.expect.anchor %>”)</span><% } %></dd>
      <dt>Type</dt><dd><%= s.found ? s.rel : '—' %><% if (l.expect && l.expect.rel && s.found && s.rel !== l.expect.rel) { %> <span class="rr-meta">(agreed: <%= l.expect.rel %>)</span><% } %></dd>
      <dt>Page</dt><dd><%= s.http ? 'HTTP ' + s.http : '—' %><%= s.noindex ? ' · set to noindex' : s.http ? ' · indexable' : '' %><% if (s.finalUrl && s.finalUrl !== l.sourceUrl) { %><div class="rr-meta">Redirects to <%= s.finalUrl %></div><% } %><% if (s.canonical && lb.host(s.canonical) && s.canonical.replace(/\/$/, '') !== (s.finalUrl || l.sourceUrl).replace(/\/$/, '')) { %><div class="rr-meta">Canonical: <%= s.canonical %></div><% } %></dd>
      <dt>Placed</dt><dd><%= lb.dmy(l.placedAt) %><%= l.placedBy ? ' · by ' + l.placedBy : '' %></dd>
      <dt>First verified</dt><dd><%= l.firstVerifiedAt ? lb.dmy(l.firstVerifiedAt) : 'not yet' %><%= l.counted ? ' · counted in ' + new Date(l.countedAt).toLocaleDateString('en-GB', { month: 'long', year: 'numeric' }) : '' %></dd>
      <dt>Last success</dt><dd><%= l.lastOkAt ? h.date(l.lastOkAt) + ', ' + h.time(l.lastOkAt) : '—' %></dd>
      <% if (gp) { %><dt>Guest post</dt><dd><a href="/link-building/posts/<%= gp._id %>"><%= gp.title || 'Open' %></a></dd><% } %>
    </dl></div>
    <% if (st && l.status !== 'replaced') { %>
    <div class="rp-note" style="display:flex; gap:8px; flex-wrap:wrap; align-items:center">
      <% if (l.status === 'review') { %><form class="js-rep" method="POST" action="/api/links/backlinks/<%= l._id %>"><input type="hidden" name="status" value="live"><button class="btn btn-sm" type="submit">Looked at it: keep as live</button></form><% } %>
      <form class="js-rep" method="POST" action="/api/links/backlinks/<%= l._id %>" style="display:flex; gap:6px" data-confirm="Mark this link as replaced? It stops being checked."><input type="hidden" name="status" value="replaced"><input class="rp-in" name="note" placeholder="Replaced by… (optional)"><button class="btn btn-sm" type="submit">Mark replaced</button></form>
      <details style="flex-basis:100%"><summary class="rr-meta" style="cursor:pointer">Edit what was agreed</summary>
        <form class="js-rep rep-form" method="POST" action="/api/links/backlinks/<%= l._id %>" style="margin-top:8px">
          <div><label>Anchor</label><input name="anchor" value="<%= (l.expect && l.expect.anchor) || '' %>"></div>
          <div><label>Type</label><select name="rel"><% ['follow', 'nofollow', 'sponsored', 'ugc'].forEach(function (r) { %><option<%= (l.expect && l.expect.rel || 'follow') === r ? ' selected' : '' %>><%= r %></option><% }) %></select></div>
          <div><label>Placed on</label><input type="date" name="placedAt" value="<%= l.placedAt ? new Date(l.placedAt).toISOString().slice(0, 10) : '' %>"></div>
          <div><label>Placed by</label><input name="placedBy" value="<%= l.placedBy || '' %>"></div>
          <div><button class="btn btn-sm" type="submit">Save</button></div>
        </form></details>
    </div>
    <% } %>
  </section>

  <section class="card b5 rp-sec">
    <div class="rp-sh"><div><div class="rp-kick">AI Citations</div><h3><%= cite ? (cite.status === 'active' ? 'Cited by AI' : 'Was cited by AI') : 'Not detected' %></h3></div></div>
    <% if (!cite) { %><div class="rp-empty">No AI answer to the questions we track for <%= brand %> cites this article yet.</div><% } else { %>
    <ul class="rp-list"><% (cite.obs || []).forEach(function (o) { %><li class="rp-item" style="grid-template-columns:minmax(0, 1fr) auto"><div><div class="rp-src"><b style="color:var(--ink)"><%= o.engine %></b><%= o.lang ? ' · ' + o.lang : '' %><%= o.branded && o.branded !== 'unknown' ? ' · ' + (o.branded === 'branded' ? 'Branded' : 'Non-branded') : '' %></div><div class="rp-sn" <%- h.dirAttr(o.prompt) %>><%= o.prompt ? '“' + o.prompt + '”' : 'No prompt data' %></div><div class="rp-why">First seen <%= lb.dmy(o.firstAt) %><%= l.placedAt && o.firstAt ? ' · ' + lb.gap(l.placedAt, o.firstAt) : '' %></div></div><div class="rp-side"><span class="rp-lab sc-<%= o.active ? 'approved' : 'dismissed' %>"><%= o.active ? 'Cited now' : 'Not now' %></span></div></li><% }) %></ul>
    <% } %>
  </section>

  <% if ((l.changes || []).length) { %>
  <section class="card b12 rp-sec">
    <div class="rp-sh"><div><h3>Changes the publisher made</h3></div></div>
    <div class="lb-wrap"><table class="lb-tbl"><thead><tr><th>When</th><th>What</th><th>From</th><th>To</th></tr></thead><tbody>
      <% l.changes.slice().reverse().forEach(function (c) { %><tr><td><%= lb.dmy(c.at) %></td><td><%= c.field === 'rel' ? 'Link type' : 'Anchor' %></td><td><%= c.from %></td><td><b><%= c.to %></b></td></tr><% }) %>
    </tbody></table></div>
  </section>
  <% } %>

  <section class="card b7 rp-sec">
    <div class="rp-sh"><div><h3>Check history</h3></div><div class="rp-sm"><%= (l.checks || []).length %> kept</div></div>
    <% if (!(l.checks || []).length) { %><div class="rp-empty">Not checked yet.</div><% } else { %>
    <div class="lb-wrap"><table class="lb-tbl"><thead><tr><th>When</th><th>Page</th><th>Link</th><th>Note</th></tr></thead><tbody>
      <% l.checks.slice().reverse().slice(0, 30).forEach(function (c) { %><tr><td style="white-space:nowrap"><%= h.date(c.at) %>, <%= h.time(c.at) %></td><td><%= c.http ? c.http : '—' %></td><td><%= c.found ? '✓ ' + (c.rel || '') : (c.http ? '✗' : '?') %></td><td class="rr-meta"><%= c.note || '' %></td></tr><% }) %>
    </tbody></table></div>
    <% } %>
  </section>

  <section class="card b5 rp-sec">
    <div class="rp-sh"><div><h3>History</h3></div></div>
    <div class="rp-body"><ul class="rep-log"><% (l.log || []).slice().reverse().forEach(function (e) { %><li><b><%= e.by %></b> <%= e.what %> <span class="rr-meta">· <%= h.date(e.at) %></span></li><% }) %></ul></div>
  </section>
</div>
<%- include('../partials/rep-foot') %>
