<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><%= service.name %> Reward</title>
    <link rel="stylesheet" href="/css/admin.css">
  </head>
  <body>
    <main class="shell explorer-shell">
      <%
        function displayCoinSymbol(symbol) {
          const raw = String(symbol || '').trim();
          const upper = raw.toUpperCase();
          if (upper === 'AIS' || upper === 'PLAI' || upper === 'PLAIS') return 'plais';
          return raw || 'plais';
        }
      %>
      <header class="topbar">
        <div>
          <p class="eyebrow"><%= service.mode %></p>
          <h1>Reward Detail</h1>
        </div>
        <div class="top-actions">
          <a class="nav-button subtle" href="/explorer">Explorer</a>
          <a class="nav-button subtle" href="/admin/rewards">Rewards</a>
          <a class="nav-button subtle" href="/admin">Admin</a>
        </div>
      </header>

      <% if (!reward) { %>
        <section class="panel">
          <div class="panel-title"><h2>Reward Not Found</h2><span>missing</span></div>
          <p class="empty">No reward ledger entry matched this id.</p>
        </section>
      <% } else { %>
        <section class="panel">
          <div class="panel-title">
            <h2><%= reward.category %> / <%= reward.rewardAmount %> <%= displayCoinSymbol(reward.symbol) %></h2>
            <span><%= reward.mode %></span>
          </div>
          <div class="chain-strip">
            <div><span>Source</span><strong><%= reward.sourceService %></strong></div>
            <div><span>Action</span><strong><%= reward.actionType %></strong></div>
            <div><span>Actor</span><code><%= reward.actorId || '-' %></code></div>
            <div><span>Block</span><strong><%= reward.blockHeight ? ('#' + reward.blockHeight) : '-' %></strong></div>
            <div><span>Created</span><strong><%= reward.createdAt || '-' %></strong></div>
          </div>
          <% if (reward.blockHeight) { %>
            <p><a class="nav-button" href="/explorer/legacy/block/<%= encodeURIComponent(reward.blockHeight) %>">Open Legacy Block #<%= reward.blockHeight %></a></p>
          <% } %>
          <% if (audit) { %>
            <p><a class="nav-button subtle" href="/explorer/reward-audit/<%= encodeURIComponent(audit.id) %>">Open Reward Audit</a></p>
          <% } %>
          <% if (reviewDecision) { %>
            <p><a class="nav-button subtle" href="/explorer/reward-review/<%= encodeURIComponent(reviewDecision.id) %>">Open Review Decision</a></p>
          <% } %>
        </section>

        <section class="grid">
          <article class="panel">
            <div class="panel-title"><h2>Validation</h2><span><%= reward.metadata?.sourcePolicyValidation?.ok ? 'ok' : 'review' %></span></div>
            <div class="item">
              <strong>Flags</strong>
              <span><%= (reward.flags || []).length ? reward.flags.join(', ') : 'none' %></span>
            </div>
            <% if (reward.metadata && reward.metadata.sourcePolicyValidation) { %>
              <div class="item">
                <strong>Source Policy</strong>
                <span>flags: <%= (reward.metadata.sourcePolicyValidation.flags || []).join(', ') || 'none' %></span>
                <span>warnings: <%= (reward.metadata.sourcePolicyValidation.warnings || []).join(', ') || 'none' %></span>
              </div>
            <% } %>
          </article>

          <article class="panel">
            <div class="panel-title"><h2>References</h2><span>trace</span></div>
            <div class="item"><strong>Reward ID</strong><code><%= reward.id %></code></div>
            <div class="item"><strong>Ref</strong><span><%= reward.refType || '-' %> / <%= reward.refId || '-' %></span></div>
            <div class="item"><strong>Idempotency</strong><code><%= reward.metadata?.idempotencyKey || '-' %></code></div>
            <div class="item"><strong>External Proof</strong><code><%= shortHash(reward.metadata?.externalProofHash || '-') %></code></div>
          </article>
        </section>

        <section class="panel">
          <div class="panel-title"><h2>Scores</h2><span>weighted</span></div>
          <% const scores = reward.metadata?.scores || {}; %>
          <% Object.keys(scores).forEach(function(key) { %>
            <div class="item">
              <strong><%= key %></strong>
              <span><%= scores[key] %></span>
            </div>
          <% }) %>
        </section>

        <% if (reward.metadata && reward.metadata.capReview) { %>
          <section class="panel">
            <div class="panel-title"><h2>Cap Review</h2><span><%= (reward.flags || []).some(function(flag) { return flag.indexOf('CAP') >= 0; }) ? 'applied' : 'clear' %></span></div>
            <div class="chain-strip">
              <div><span>Day</span><strong><%= reward.metadata.capReview.dayKey %></strong></div>
              <div><span>Requested</span><strong><%= reward.metadata.capReview.requestedAmount %> <%= displayCoinSymbol(reward.symbol) %></strong></div>
              <div><span>Applied</span><strong><%= reward.metadata.capReview.cappedAmount %> <%= displayCoinSymbol(reward.symbol) %></strong></div>
              <div><span>User Cap</span><strong><%= reward.metadata.capReview.dailyUserCap %></strong></div>
              <div><span>Category Cap</span><strong><%= reward.metadata.capReview.dailyCategoryCap %></strong></div>
            </div>
          </section>
        <% } %>

        <section class="panel">
          <div class="panel-title">
            <h2>Review Decision</h2>
            <span><%= reviewDecision ? reviewDecision.decision : 'none' %></span>
          </div>
          <% if (!reviewDecision) { %>
            <p class="empty">No operator review decision has been recorded for this reward.</p>
          <% } else { %>
            <div class="chain-strip">
              <div><span>Decision</span><strong><%= reviewDecision.decision %></strong></div>
              <div><span>Reviewer</span><strong><%= reviewDecision.reviewer || '-' %></strong></div>
              <div><span>Previous Amount</span><strong><%= reviewDecision.previousRewardAmount %> <%= displayCoinSymbol(reward.symbol) %></strong></div>
              <div><span>Reviewed</span><strong><%= reviewDecision.reviewedAt || reviewDecision.createdAt || '-' %></strong></div>
              <div><span>Chain Status</span><strong><%= reviewDecision.chainStatus || '-' %></strong></div>
              <div><span>Ledger Block</span><strong><%= reviewDecision.chainLedgerBlockHeight ? ('#' + reviewDecision.chainLedgerBlockHeight) : '-' %></strong></div>
            </div>
            <div class="item">
              <strong>Reason</strong>
              <span><%= reviewDecision.reason || '-' %></span>
            </div>
            <div class="item">
              <strong>Decision ID</strong>
              <a href="/explorer/reward-review/<%= encodeURIComponent(reviewDecision.id) %>"><code><%= reviewDecision.id %></code></a>
            </div>
            <div class="item">
              <strong>Review Chain Transaction</strong>
              <% if (reviewDecisionTx) { %>
                <a href="/explorer/tx/<%= encodeURIComponent(reviewDecisionTx.txHash || reviewDecisionTx.id) %>"><code><%= shortHash(reviewDecisionTx.txHash || reviewDecisionTx.id, 18, 12) %></code></a>
                <span><%= reviewDecisionTx.status || '-' %> / ledger block <%= reviewDecisionTx.ledgerBlockHeight || reviewDecisionTx.blockHeight || '-' %></span>
              <% } else { %>
                <code><%= reviewDecision.txHash || reviewDecision.chainTransactionId || '-' %></code>
              <% } %>
            </div>
            <% if (reviewDecision.chainLedgerBlockHeight) { %>
              <div class="item">
                <strong>Ledger Block Anchor</strong>
                <a href="/explorer/legacy/block/<%= encodeURIComponent(reviewDecision.chainLedgerBlockHeight) %>"><code>#<%= reviewDecision.chainLedgerBlockHeight %></code></a>
              </div>
            <% } %>
          <% } %>
        </section>
      <% } %>
    </main>
  </body>
</html>
