<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><%= service.name %> Reward Audit</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 Audit 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 (!audit) { %>
        <section class="panel">
          <div class="panel-title"><h2>Audit Not Found</h2><span>missing</span></div>
          <p class="empty">No reward audit log matched this id or hash.</p>
        </section>
      <% } else { %>
        <section class="panel">
          <div class="panel-title">
            <h2><%= audit.sourceService %> / <%= audit.actionType %></h2>
            <span><%= audit.status %></span>
          </div>
          <div class="chain-strip">
            <div><span>Category</span><strong><%= audit.category %></strong></div>
            <div><span>Actor</span><code><%= audit.actorId || '-' %></code></div>
            <div><span>Validation</span><strong><%= audit.validation && audit.validation.ok ? 'OK' : 'REVIEW' %></strong></div>
            <div><span>Created</span><strong><%= audit.createdAt || '-' %></strong></div>
          </div>
          <div class="item">
            <strong>Request Hash</strong>
            <code><%= audit.requestHash || '-' %></code>
          </div>
        </section>

        <section class="grid">
          <article class="panel">
            <div class="panel-title"><h2>Reward Link</h2><span>ledger</span></div>
            <div class="item">
              <strong>Reward ID</strong>
              <% if (reward) { %>
                <a href="/explorer/reward/<%= encodeURIComponent(reward.id) %>"><code><%= reward.id %></code></a>
              <% } else { %>
                <code><%= audit.rewardId || '-' %></code>
              <% } %>
            </div>
            <div class="item">
              <strong>Ref / Idempotency</strong>
              <span><%= audit.refId || '-' %></span>
              <code><%= audit.idempotencyKey || '-' %></code>
            </div>
          </article>

          <article class="panel">
            <div class="panel-title"><h2>Service Identity</h2><span>auth</span></div>
            <div class="item">
              <strong><%= audit.serviceIdentity ? audit.serviceIdentity.serviceName : '-' %></strong>
              <span>authenticated: <%= audit.serviceIdentity && audit.serviceIdentity.authenticated ? 'yes' : 'no' %></span>
              <span>mode: <%= audit.serviceIdentity ? audit.serviceIdentity.authMode : '-' %></span>
            </div>
          </article>
        </section>

        <section class="grid">
          <article class="panel">
            <div class="panel-title"><h2>Validation</h2><span>policy</span></div>
            <div class="item">
              <strong>Flags</strong>
              <span><%= audit.validation && audit.validation.flags && audit.validation.flags.length ? audit.validation.flags.join(', ') : 'none' %></span>
            </div>
            <div class="item">
              <strong>Warnings</strong>
              <span><%= audit.validation && audit.validation.warnings && audit.validation.warnings.length ? audit.validation.warnings.join(', ') : 'none' %></span>
            </div>
          </article>

          <article class="panel">
            <div class="panel-title"><h2>Reward Result</h2><span>simulation</span></div>
            <div class="item">
              <strong><%= audit.metadata && audit.metadata.rewardAmount ? audit.metadata.rewardAmount : 0 %> <%= displayCoinSymbol(reward ? reward.symbol : 'plais') %></strong>
              <span>score: <%= audit.metadata && audit.metadata.rewardScore ? audit.metadata.rewardScore : 0 %></span>
              <span>mode: <%= audit.metadata && audit.metadata.rewardMode ? audit.metadata.rewardMode : '-' %></span>
              <span>flags: <%= audit.metadata && audit.metadata.rewardFlags && audit.metadata.rewardFlags.length ? audit.metadata.rewardFlags.join(', ') : 'none' %></span>
            </div>
          </article>
        </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 audit.</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 ? reward.symbol : 'plais') %></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>
