<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><%= service.name %> Rewards</title>
    <link rel="stylesheet" href="/css/admin.css">
  </head>
  <body>
    <main class="shell">
      <%
        function fmtNumber(value, digits) {
          const num = Number(value || 0);
          if (!Number.isFinite(num)) return '0';
          const options = typeof digits === 'number'
            ? { minimumFractionDigits: digits, maximumFractionDigits: digits }
            : { maximumFractionDigits: 6 };
          return num.toLocaleString('en-US', options);
        }
      %>
      <header class="topbar">
        <div>
          <p class="eyebrow"><%= service.mode %></p>
          <h1>Rewards</h1>
        </div>
        <div class="top-actions">
          <a class="nav-button" href="/admin">Admin</a>
          <a class="nav-button" href="https://aisfor.kr/chain-wallet?next=/wallet">Wallet</a>
          <a class="nav-button" href="/explorer">Explorer</a>
          <a class="nav-button" href="/admin/ledger-migration">Ledger Migration / Rollback</a>
          <a class="nav-button subtle" href="/admin/logout">Logout</a>
          <div class="status"><span></span>Port <%= service.port %></div>
        </div>
      </header>

      <section class="grid metrics" aria-label="Reward metrics">
        <article><strong><%= rewardPolicy.tokenomics.config.symbol %></strong><span>Symbol</span></article>
        <article><strong><%= rewardPolicy.tokenomics.config.model %></strong><span>Reward Model</span></article>
        <article><strong><%= rewardPolicy.tokenomics.config.rewardMode %></strong><span>Mode</span></article>
        <article><strong><%= fmtNumber(rewardPolicy.tokenomics.config.maxSupply) %></strong><span>Max Supply</span></article>
        <article><strong><%= fmtNumber(rewardPolicy.tokenomics.limits.dailyUserCap) %></strong><span>Daily User Cap</span></article>
        <article><strong><%= fmtNumber(rewardPolicy.tokenomics.limits.dailyCategoryCap) %></strong><span>Daily Category Cap</span></article>
        <article><strong><%= rewardStats.entryCount %></strong><span>Simulation Entries</span></article>
        <article><strong><%= rewardAuditStats.entryCount %></strong><span>Audit Logs</span></article>
        <article><strong><%= rewardStats.reviewCount %></strong><span>Review Queue</span></article>
        <article><strong><%= rewardStats.resolvedReviewCount || 0 %></strong><span>Resolved Reviews</span></article>
        <article><strong><%= rewardStats.capAppliedCount %></strong><span>Cap Applied</span></article>
        <article><strong><%= rewardReviewStats.decisionCount %></strong><span>Review Decisions</span></article>
        <article><strong><%= rewardReviewStats.anchoredCount || 0 %></strong><span>Anchored Reviews</span></article>
        <article><strong><%= rewardReviewStats.pendingAnchorCount || 0 %></strong><span>Pending Anchors</span></article>
        <article><strong><%= rewardReviewStats.missingTxCount || 0 %></strong><span>Missing Review Tx</span></article>
        <article><strong><%= fmtNumber(rewardStats.totalSimulatedReward, 6) %></strong><span>Simulated Reward</span></article>
        <article><strong><%= rewardStats.flaggedCount %></strong><span>Flagged</span></article>
      </section>

      <section class="panel">
        <div class="panel-title">
          <h2>Reward Operations Readiness</h2>
          <span><%= rewardReadiness.status %> / <%= rewardReadiness.readinessScore %>%</span>
        </div>
        <div class="chain-strip">
          <div><span>Rewards</span><strong><%= rewardReadiness.summary.rewardEntries %></strong></div>
          <div><span>Audit Logs</span><strong><%= rewardReadiness.summary.auditLogs %></strong></div>
          <div><span>Review Queue</span><strong><%= rewardReadiness.summary.reviewQueue %></strong></div>
          <div><span>Review Anchors</span><strong><%= rewardReadiness.summary.anchoredReviews %></strong></div>
          <div><span>Manifest Samples</span><strong><%= rewardReadiness.summary.integrationSamples || 0 %></strong></div>
          <div><span>Sample Warnings</span><strong><%= rewardReadiness.summary.integrationSampleWarnings || 0 %></strong></div>
          <div><span>Dry-run Samples</span><strong><%= rewardReadiness.summary.dryRunSamples || 0 %></strong></div>
          <div><span>HTTP Smoke</span><strong><%= rewardReadiness.summary.httpDryRunSmokePassed || 0 %>/<%= rewardReadiness.summary.httpDryRunSmokeServices || 0 %></strong></div>
          <div><span>Live Canary</span><strong><%= rewardReadiness.summary.liveCanaryOk ? (rewardReadiness.summary.liveCanaryLive ? 'LIVE' : 'DRY') : 'WAIT' %></strong></div>
          <div><span>Canary Preflight</span><strong><%= rewardReadiness.summary.liveCanaryPreflightOk ? 'READY' : 'WAIT' %></strong></div>
          <div><span>Canary Postcheck</span><strong><%= rewardReadiness.summary.liveCanaryPostCheckOk ? 'OK' : 'WATCH' %></strong></div>
          <div><span>Canary Permit</span><strong><%= rewardReadiness.summary.liveCanaryPermitOk ? 'PERMIT' : 'LOCKED' %></strong></div>
          <div><span>Rollout Ready</span><strong><%= rewardReadiness.summary.rolloutReadyServices || 0 %>/<%= rewardReadiness.summary.rolloutTotalServices || 0 %></strong></div>
          <div><span>Blocks</span><strong><%= rewardReadiness.summary.totalBlocks %></strong></div>
          <div><span>Finalized</span><strong><%= rewardReadiness.summary.finalizedBlocks %></strong></div>
          <div><span>Auth Keys</span><strong><%= rewardReadiness.summary.authConfiguredServices || 0 %>/<%= rewardReadiness.summary.authExternalServices || 0 %></strong></div>
        </div>
        <% rewardReadiness.checks.forEach(function(check) { %>
          <div class="item">
            <strong><%= check.label %> / <%= check.status %></strong>
            <span><%= check.detail %></span>
            <code><%= check.key %> / <%= check.severity %></code>
          </div>
        <% }) %>
      </section>

      <section class="panel">
        <div class="panel-title">
          <h2>Testnet Policy Readiness</h2>
          <span><%= testnetStatus.config.networkId %> / <%= testnetStatus.readiness.status %></span>
        </div>
        <div class="chain-strip">
          <div><span>Readiness</span><strong><%= testnetStatus.readiness.score %>%</strong></div>
          <div><span>Chain</span><strong><%= testnetStatus.config.chainName %></strong></div>
          <div><span>Symbol</span><strong><%= testnetStatus.config.nativeSymbol %></strong></div>
          <div><span>Issuance</span><strong><%= testnetStatus.config.coinIssuance %></strong></div>
          <div><span>Validators</span><strong><%= testnetStatus.validators.length %></strong></div>
          <div><span>Latest Block</span><strong><%= testnetStatus.latestBlock ? testnetStatus.latestBlock.height : 0 %></strong></div>
        </div>
        <div class="item">
          <strong>Policy Document</strong>
          <span>Internal PLAI Point -> controlled testnet token rehearsal -> mainnet coin policy review.</span>
          <span>No public issuance, exchange, withdrawal, or monetary-value claim is enabled by this testnet policy.</span>
          <code>docs/TESTNET_POLICY.md / docs/MAINNET_POLICY_DRAFT.md</code>
        </div>
        <div class="item">
          <strong>Genesis Preview</strong>
          <span>Version <%= testnetStatus.genesis.genesisVersion %> / created <%= testnetStatus.genesis.createdAt %></span>
          <code><%= testnetStatus.genesis.genesisHash %></code>
        </div>
        <% testnetStatus.readiness.checks.forEach(function(check) { %>
          <div class="item">
            <strong><%= check.label %> / <%= check.ok ? 'OK' : 'WATCH' %></strong>
            <span><%= check.detail %></span>
            <code><%= check.key %></code>
          </div>
        <% }) %>
      </section>

      <section class="panel">
        <div class="panel-title">
          <h2>Reward Live Canary</h2>
          <span><%= rewardReadiness.liveCanaryPreflight.status %> / <%= rewardReadiness.latestLiveCanary.ok ? rewardReadiness.latestLiveCanary.mode : 'not ready' %></span>
        </div>
        <div class="item">
          <strong><%= rewardReadiness.latestLiveCanary.serviceName || 'No canary result yet' %></strong>
          <span>Live write: <%= rewardReadiness.latestLiveCanary.live ? 'yes' : 'no' %> / checked <%= rewardReadiness.latestLiveCanary.checkedAt || '-' %></span>
          <span>Reward <%= fmtNumber(rewardReadiness.latestLiveCanary.rewardAmount || 0, 6) %> <%= rewardPolicy.tokenomics.config.symbol %> / review <%= rewardReadiness.latestLiveCanary.wouldNeedReview ? 'yes' : 'no' %></span>
          <span>Reward ID: <%= rewardReadiness.latestLiveCanary.rewardId || '-' %> / Audit ID: <%= rewardReadiness.latestLiveCanary.auditId || '-' %></span>
          <code><%= rewardReadiness.latestLiveCanary.idempotencyKey || rewardReadiness.latestLiveCanary.detail || 'Run scripts/reward-live-canary.js first.' %></code>
          <code>Preflight: <%= rewardReadiness.liveCanaryPreflight.recommendedNextAction %></code>
        </div>
        <% (rewardReadiness.liveCanaryPreflight.checks || []).forEach(function(check) { %>
          <div class="item">
            <strong><%= check.label %> / <%= check.status %></strong>
            <span><%= check.detail %></span>
            <code><%= check.key %> / <%= check.severity %></code>
          </div>
        <% }) %>
        <div class="item">
          <strong>Post-check / <%= rewardReadiness.liveCanaryPostCheck.ok ? 'OK' : 'WATCH' %></strong>
          <span>Mode <%= rewardReadiness.liveCanaryPostCheck.canaryMode %> / live <%= rewardReadiness.liveCanaryPostCheck.live ? 'yes' : 'no' %></span>
          <span>Reward ID: <%= rewardReadiness.liveCanaryPostCheck.rewardId || '-' %> / Audit ID: <%= rewardReadiness.liveCanaryPostCheck.auditId || '-' %></span>
          <code><%= rewardReadiness.liveCanaryPostCheck.idempotencyKey || '-' %></code>
        </div>
        <% (rewardReadiness.liveCanaryPostCheck.checks || []).forEach(function(check) { %>
          <div class="item">
            <strong><%= check.key %> / <%= check.ok ? 'OK' : 'WATCH' %></strong>
            <span><%= check.detail %></span>
          </div>
        <% }) %>
        <div class="item">
          <strong>Execution Permit / <%= rewardReadiness.liveCanaryPermit.status %></strong>
          <span>Service <%= rewardReadiness.liveCanaryPermit.serviceName || '-' %> / permit <%= rewardReadiness.liveCanaryPermit.permitId %></span>
          <span><%= rewardReadiness.liveCanaryPermit.note %></span>
          <code><%= rewardReadiness.liveCanaryPermit.dryRunCommand || 'Run dry-run canary first.' %></code>
          <code><%= rewardReadiness.liveCanaryPermit.liveCommand || 'Live command locked.' %></code>
        </div>
        <% (rewardReadiness.liveCanaryPermit.checks || []).forEach(function(check) { %>
          <div class="item">
            <strong><%= check.label %> / <%= check.status %></strong>
            <span><%= check.detail %></span>
            <code><%= check.key %> / <%= check.severity %></code>
          </div>
        <% }) %>
        <div class="item">
          <strong>Operator Approval / <%= rewardLiveCanaryApproval.approved ? 'APPROVED' : 'NOT APPROVED' %></strong>
          <span>Service <%= rewardLiveCanaryApproval.serviceName || '-' %> / permit <%= rewardLiveCanaryApproval.permitId || '-' %></span>
          <span>Approved <%= rewardLiveCanaryApproval.approvedAt || '-' %> by <%= rewardLiveCanaryApproval.approvedBy || '-' %></span>
          <code><%= rewardLiveCanaryApproval.approvalId || 'No approval recorded.' %></code>
          <code><%= rewardLiveCanaryApproval.liveCommand || rewardReadiness.liveCanaryPermit.liveCommand || 'Live command locked.' %></code>
          <form class="inline-form" method="post" action="/admin/rewards/live-canary/approve">
            <button type="submit">Record Canary Approval</button>
          </form>
          <form class="inline-form" method="post" action="/admin/rewards/live-canary/revoke">
            <button type="submit">Revoke Canary Approval</button>
          </form>
        </div>
      </section>

      <section class="panel">
        <div class="panel-title">
          <h2>Reward Service Rollout</h2>
          <span><%= rewardReadiness.summary.rolloutConnectedServices || 0 %> connected / <%= rewardReadiness.summary.rolloutReadyServices || 0 %> ready</span>
        </div>
        <% (rewardReadiness.rolloutPlan || []).forEach(function(row) { %>
          <div class="item">
            <strong><%= row.serviceName %> / <%= row.status %></strong>
            <span>Source: <%= row.sourceStatus %> / auth <%= row.authConfigured ? 'configured' : 'missing' %> / smoke <%= row.dryRunSmokeOk ? 'ok' : 'watch' %></span>
            <span>Dry-run reward <%= fmtNumber(row.rewardAmount, 6) %> <%= rewardPolicy.tokenomics.config.symbol %> / review <%= row.wouldNeedReview ? 'yes' : 'no' %></span>
            <span><%= row.nextAction %></span>
            <code><%= (row.flags || []).length ? row.flags.join(', ') : 'flags: none' %></code>
          </div>
        <% }) %>
      </section>

      <section class="panel">
        <div class="panel-title">
          <h2>PoSS Policy</h2>
          <span><%= rewardPolicy.tokenomics.config.modelFullName %></span>
        </div>
        <div class="chain-strip">
          <div><span>Reward Unit</span><strong><%= rewardPolicy.tokenomics.config.rewardUnitName %></strong></div>
          <div><span>Display Name</span><strong><%= rewardPolicy.tokenomics.config.tokenDisplayName %></strong></div>
          <div><span>Phase</span><strong><%= rewardPolicy.tokenomics.config.phase %></strong></div>
          <div><span>Public Issuance</span><strong><%= rewardPolicy.tokenomics.config.publicIssuance %></strong></div>
        </div>
        <p class="muted"><%= rewardPolicy.settlement.note %></p>
      </section>

      <section class="panel">
        <div class="panel-title">
          <h2>Reward Source Readiness</h2>
          <span><%= rewardPolicy.sources.length %> services</span>
        </div>
        <% rewardReadiness.sourceReadiness.forEach(function(source) { %>
          <div class="item">
            <strong><%= source.serviceName %> / <%= source.status %></strong>
            <span>Categories: <%= source.categories.join(', ') %></span>
            <span>Events: <%= source.eventTypes.join(', ') %></span>
            <span>Auth: <%= source.authConfigured ? 'configured' : 'missing' %> / <%= source.authEnvKey %></span>
            <code><%= source.endpoint %> / <%= source.idempotency %></code>
            <span><%= source.note %></span>
          </div>
        <% }) %>
      </section>

      <section class="panel">
        <div class="panel-title">
          <h2>Reward Integration Manifest</h2>
          <span><%= rewardManifest.version %></span>
        </div>
        <div class="chain-strip">
          <div><span>Endpoint</span><strong><%= rewardManifest.method %> <%= rewardManifest.endpoint %></strong></div>
          <div><span>Dry-run</span><strong><%= rewardManifest.method %> <%= rewardManifest.dryRunEndpoint %></strong></div>
          <div><span>Auth</span><strong><%= rewardManifest.auth.required ? 'required' : 'optional' %></strong></div>
          <div><span>Auth Mode</span><strong><%= rewardManifest.auth.mode %></strong></div>
          <div><span>Services</span><strong><%= rewardManifest.services.length %></strong></div>
          <div><span>Mode</span><strong><%= rewardPolicy.tokenomics.config.rewardMode %></strong></div>
        </div>
        <div class="item">
          <strong>Required fields</strong>
          <span><%= rewardManifest.requiredFields.join(', ') %></span>
          <code>Recommended: <%= rewardManifest.recommendedFields.join(', ') %></code>
          <code>Client helper: <%= rewardManifest.auth.helper %></code>
        </div>
        <% rewardManifest.services.forEach(function(source) { %>
          <div class="item">
            <strong><%= source.serviceName %> / <%= source.status %></strong>
            <span>Categories: <%= source.categories.join(', ') %></span>
            <span>Events: <%= source.eventTypes.join(', ') %></span>
            <code><%= source.idempotency %></code>
            <% if ((source.samplePayloads || []).length) { %>
              <code><%= JSON.stringify(source.samplePayloads[0], null, 2) %></code>
            <% } %>
          </div>
        <% }) %>
      </section>

      <section class="panel">
        <div class="panel-title">
          <h2>Policy Simulation</h2>
          <span>category default score</span>
        </div>
        <form class="inline-form" method="post" action="/admin/rewards/simulate-policy">
          <input name="actorId" placeholder="Actor ID" value="policy-simulated-user">
          <select name="sourceService">
            <option value="AISFOR_CHAIN">AISFOR_CHAIN</option>
            <option value="AISFOR">AISFOR</option>
            <option value="AISTALK">AISTALK</option>
            <option value="AISGROUP">AISGROUP</option>
            <option value="AISMALL">AISMALL</option>
          </select>
          <select name="category">
            <% rewardPolicy.categories.forEach(function(policy) { %>
              <option value="<%= policy.key %>"><%= policy.label %></option>
            <% }) %>
          </select>
          <input name="actionType" placeholder="Action type" value="POLICY_SIMULATION">
          <input name="baseScore" type="number" step="0.000001" min="0" placeholder="Base score" value="10">
          <input name="note" placeholder="Note" value="Policy simulation">
          <button type="submit">Simulate</button>
        </form>
      </section>

      <section class="grid">
        <article class="panel">
          <div class="panel-title"><h2>Reward Categories</h2><span><%= rewardPolicy.categories.length %> categories</span></div>
          <% rewardPolicy.categories.forEach(function(policy) { %>
            <div class="item">
              <strong><%= policy.label %> / <%= policy.key %></strong>
              <span><%= policy.description %></span>
              <span>Default score <%= policy.defaultScore %> / events <%= policy.sampleEvents.join(', ') %></span>
              <code><%= policy.abuseControls.join(' / ') %></code>
            </div>
          <% }) %>
        </article>

        <article class="panel">
          <div class="panel-title"><h2>Category Totals</h2><span><%= rewardPolicy.tokenomics.config.symbol %></span></div>
          <% Object.keys(rewardStats.totalsByCategory).forEach(function(category) { %>
            <div class="item">
              <strong><%= category %></strong>
              <span><%= fmtNumber(rewardStats.totalsByCategory[category], 6) %> <%= rewardPolicy.tokenomics.config.symbol %></span>
            </div>
          <% }) %>
        </article>
      </section>

      <section class="grid">
        <article class="panel">
          <div class="panel-title"><h2>Top Actors</h2><span>simulation</span></div>
          <% if ((rewardStats.totalsByActor || []).length === 0) { %><p class="empty">No reward actors yet.</p><% } %>
          <% (rewardStats.totalsByActor || []).forEach(function(row) { %>
            <div class="item">
              <strong><%= row.actorId %></strong>
              <span><%= fmtNumber(row.rewardAmount, 6) %> <%= rewardPolicy.tokenomics.config.symbol %></span>
            </div>
          <% }) %>
        </article>

        <article class="panel">
          <div class="panel-title"><h2>Latest Reward Ledger</h2><span>latest <%= (rewardStats.latestEntries || []).length %></span></div>
          <% if ((rewardStats.latestEntries || []).length === 0) { %><p class="empty">No reward simulations yet.</p><% } %>
          <% (rewardStats.latestEntries || []).forEach(function(entry) { %>
            <div class="item">
              <strong><%= entry.category %> / <%= fmtNumber(entry.rewardAmount, 6) %> <%= entry.symbol %></strong>
              <span><%= entry.sourceService %> / <%= entry.actionType %> / <%= entry.mode %></span>
              <span><%= (entry.flags || []).length ? ('flags: ' + entry.flags.join(', ')) : 'flags: none' %></span>
              <% if (entry.metadata && entry.metadata.sourcePolicyValidation) { %>
                <span>validation: <%= entry.metadata.sourcePolicyValidation.ok ? 'ok' : 'review' %></span>
              <% } %>
              <% if (entry.metadata && entry.metadata.capReview) { %>
                <span>cap: requested <%= fmtNumber(entry.metadata.capReview.requestedAmount, 6) %> / applied <%= fmtNumber(entry.metadata.capReview.cappedAmount, 6) %></span>
              <% } %>
              <% if (entry.metadata && entry.metadata.reviewDecision) { %>
                <span>review: <%= entry.metadata.reviewDecision.decision %> / <%= entry.metadata.reviewDecision.reviewer || '-' %></span>
              <% } %>
              <code><%= entry.id %></code>
            </div>
          <% }) %>
        </article>
      </section>

      <section class="panel">
        <div class="panel-title">
          <h2>Reward Review Queue</h2>
          <span><%= rewardReviewQueue.length %> pending</span>
        </div>
        <% if (rewardReviewQueue.length === 0) { %><p class="empty">No reward entries need review.</p><% } %>
        <% rewardReviewQueue.forEach(function(entry) { %>
          <div class="item">
            <strong><%= entry.sourceService %> / <%= entry.actionType %> / <%= fmtNumber(entry.rewardAmount, 6) %> <%= entry.symbol %></strong>
            <span><%= entry.actorId || '-' %> / <%= entry.category %> / block <%= entry.blockHeight || '-' %></span>
            <span>flags: <%= (entry.flags || []).length ? entry.flags.join(', ') : 'none' %></span>
            <% if (entry.validation) { %>
              <span>validation: <%= entry.validation.ok ? 'ok' : 'review' %></span>
            <% } %>
            <% if (entry.capReview) { %>
              <span>cap: requested <%= fmtNumber(entry.capReview.requestedAmount, 6) %> / applied <%= fmtNumber(entry.capReview.cappedAmount, 6) %></span>
            <% } %>
            <% if (entry.reviewDecision) { %>
              <span>current decision: <%= entry.reviewDecision.decision %> / <%= entry.reviewDecision.reviewer || '-' %></span>
            <% } %>
            <code><%= entry.id %></code>
            <form class="inline-form" method="post" action="/admin/rewards/<%= encodeURIComponent(entry.id) %>/review">
              <select name="decision">
                <option value="WATCH">WATCH</option>
                <option value="APPROVE">APPROVE</option>
                <option value="REJECT">REJECT</option>
              </select>
              <input name="reviewer" placeholder="Reviewer" value="admin">
              <input name="reason" placeholder="Decision note">
              <button type="submit">Record Decision</button>
            </form>
          </div>
        <% }) %>
      </section>

      <section class="panel">
        <div class="panel-title">
          <h2>Reward Review Decisions</h2>
          <span>anchored <%= rewardReviewStats.anchoredCount || 0 %> / pending <%= rewardReviewStats.pendingAnchorCount || 0 %></span>
        </div>
        <div class="chain-strip">
          <div><span>Chain Tx</span><strong><%= rewardReviewStats.chainTxCount || 0 %></strong></div>
          <div><span>Ledger Anchored</span><strong><%= rewardReviewStats.anchoredCount || 0 %></strong></div>
          <div><span>Pending Anchor</span><strong><%= rewardReviewStats.pendingAnchorCount || 0 %></strong></div>
          <div><span>Missing Tx</span><strong><%= rewardReviewStats.missingTxCount || 0 %></strong></div>
        </div>
        <form class="inline-form" method="post" action="/admin/rewards/review-anchors/repair">
          <input name="limit" type="number" min="1" max="100" value="25" aria-label="Repair limit">
          <input name="reviewer" value="admin-anchor-repair" aria-label="Repair reviewer">
          <button type="submit">Repair Review Anchors</button>
        </form>
        <% if ((rewardReviewStats.latestDecisions || []).length === 0) { %><p class="empty">No reward review decisions yet.</p><% } %>
        <% (rewardReviewStats.latestDecisions || []).forEach(function(row) { %>
          <div class="item">
            <strong><%= row.decision %> / <%= row.reviewer %></strong>
            <span>reward <%= row.rewardId %> / previous <%= fmtNumber(row.previousRewardAmount, 6) %></span>
            <span>chain: <%= row.chainStatus || '-' %> / ledger block <%= row.chainLedgerBlockHeight || '-' %> / mock block <%= row.chainMockBlockHeight || '-' %></span>
            <span><%= row.reason || 'no note' %></span>
            <a href="/explorer/reward-review/<%= encodeURIComponent(row.id) %>"><code><%= row.id %></code></a>
            <% if (row.txHash) { %>
              <a href="/explorer/tx/<%= encodeURIComponent(row.txHash) %>"><code><%= row.txHash %></code></a>
            <% } %>
            <% if (row.chainLedgerBlockHeight) { %>
              <a href="/explorer/block/<%= encodeURIComponent(row.chainLedgerBlockHeight) %>"><code>#<%= row.chainLedgerBlockHeight %></code></a>
            <% } %>
          </div>
        <% }) %>
      </section>

      <section class="panel">
        <div class="panel-title">
          <h2>Reward API Audit</h2>
          <span>latest <%= (rewardAuditStats.latestEntries || []).length %></span>
        </div>
        <% if ((rewardAuditStats.latestEntries || []).length === 0) { %><p class="empty">No reward API audit logs yet.</p><% } %>
        <% (rewardAuditStats.latestEntries || []).forEach(function(row) { %>
          <div class="item">
            <strong><%= row.sourceService %> / <%= row.actionType %> / <%= row.status %></strong>
            <span>validation: <%= row.validation && row.validation.ok ? 'ok' : 'review' %> / reward <%= row.rewardId || '-' %></span>
            <span>identity: <%= row.serviceIdentity ? (row.serviceIdentity.serviceName + ' / ' + row.serviceIdentity.authMode) : '-' %></span>
            <code><%= row.requestHash %></code>
          </div>
        <% }) %>
      </section>
    </main>
  </body>
</html>
