<?php
use MintyPHP\Module\Audit\Domain\SystemAuditOutcome;

/**
 * @var array $auditLog
 */

$auditLog = $auditLog ?? [];
$outcome = SystemAuditOutcome::normalizeOr((string) ($auditLog['outcome'] ?? ''), SystemAuditOutcome::Success);
$outcomeVariant = $outcome->badgeVariant();

$metadataJson = trim((string) ($auditLog['metadata_json'] ?? ''));
$metadataPretty = '-';
if ($metadataJson !== '') {
  $decoded = json_decode($metadataJson, true);
  if (is_array($decoded)) {
    $pretty = json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    $metadataPretty = is_string($pretty) ? $pretty : $metadataJson;
  } else {
    $metadataPretty = $metadataJson;
  }
}

$requestId = trim((string) ($auditLog['request_id'] ?? ''));
$requestIdDisplay = $requestId !== '' ? substr($requestId, 0, 10) : '-';
$eventType = trim((string) ($auditLog['event_type'] ?? ''));
$channel = strtoupper(trim((string) ($auditLog['channel'] ?? '')));
$errorCode = trim((string) ($auditLog['error_code'] ?? ''));
$method = strtoupper(trim((string) ($auditLog['method'] ?? '')));
$path = trim((string) ($auditLog['path'] ?? ''));
$targetType = trim((string) ($auditLog['target_type'] ?? ''));
$targetUuid = trim((string) ($auditLog['target_uuid'] ?? ''));
$ipHash = trim((string) ($auditLog['ip_hash'] ?? ''));
$ipHashDisplay = $ipHash !== '' ? substr($ipHash, 0, 16) . '...' : '-';
$userAgentHash = trim((string) ($auditLog['user_agent_hash'] ?? ''));
$userAgentHashDisplay = $userAgentHash !== '' ? substr($userAgentHash, 0, 16) . '...' : '-';

$actorLabel = trim((string) ($auditLog['actor_user_display_name'] ?? ''));
$actorEmail = trim((string) ($auditLog['actor_user_email'] ?? ''));
if ($actorLabel === '') {
  $actorLabel = $actorEmail !== '' ? $actorEmail : '-';
}

$tenantLabel = trim((string) ($auditLog['actor_tenant_description'] ?? ''));
if ($tenantLabel === '') {
  $tenantLabel = '-';
}
?>

<div class="app-details-container">
  <section>
    <?php
    $titlebar = [
      'title' => t('View system audit entry'),
      'backHref' => 'audit/system-audit',
      'backTitle' => t('Back'),
    ];
    require templatePath('partials/app-details-titlebar.phtml');
    ?>

    <div class="app-details-content">
      <details class="app-details-card" open>
        <summary>
          <span class="app-details-card-summary-title"><?php e(t('Event details')); ?></span>
          <span class="app-details-card-summary-meta">
            <span class="badge" data-variant="<?php e($outcomeVariant); ?>"><?php e(t($outcome->labelToken())); ?></span>
          </span>
        </summary>
        <div class="app-details-card-container">
          <div class="grid">
            <div>
              <small><?php e(t('Event type')); ?></small>
              <p><code><?php e($eventType !== '' ? $eventType : '-'); ?></code></p>
            </div>
            <div>
              <small><?php e(t('Channel')); ?></small>
              <p><?php e($channel !== '' ? $channel : '-'); ?></p>
            </div>
          </div>
          <div class="grid">
            <div>
              <small><?php e(t('Method')); ?></small>
              <p><span class="badge" data-variant="neutral"><?php e($method !== '' ? $method : '-'); ?></span></p>
            </div>
            <div>
              <small><?php e(t('Error code')); ?></small>
              <p><?php e($errorCode !== '' ? $errorCode : '-'); ?></p>
            </div>
          </div>
          <div>
            <small><?php e(t('Path')); ?></small>
            <p><code><?php e($path !== '' ? $path : '-'); ?></code></p>
          </div>
        </div>
      </details>

      <details class="app-details-card" open>
        <summary>
          <span class="app-details-card-summary-title"><?php e(t('Scope')); ?></span>
        </summary>
        <div class="app-details-card-container">
          <div class="grid">
            <div>
              <small><?php e(t('Actor')); ?></small>
              <p>
                <?php if (!empty($auditLog['actor_user_uuid'])): ?>
                  <a href="admin/users/edit/<?php e($auditLog['actor_user_uuid']); ?>"><?php e($actorLabel); ?></a>
                <?php else: ?>
                  <?php e($actorLabel); ?>
                <?php endif; ?>
              </p>
            </div>
            <div>
              <small><?php e(t('Actor tenant')); ?></small>
              <p>
                <?php if (!empty($auditLog['actor_tenant_uuid'])): ?>
                  <a href="admin/tenants/edit/<?php e($auditLog['actor_tenant_uuid']); ?>"><?php e($tenantLabel); ?></a>
                <?php else: ?>
                  <?php e($tenantLabel); ?>
                <?php endif; ?>
              </p>
            </div>
          </div>
          <div class="grid">
            <div>
              <small><?php e(t('Target type')); ?></small>
              <p><?php e($targetType !== '' ? $targetType : '-'); ?></p>
            </div>
            <div>
              <small><?php e(t('Target UUID')); ?></small>
              <p><code><?php e($targetUuid !== '' ? $targetUuid : '-'); ?></code></p>
            </div>
          </div>
        </div>
      </details>

      <?php if ($metadataPretty !== '-'): ?>
        <details class="app-details-card">
          <summary>
            <span class="app-details-card-summary-title"><?php e(t('Metadata')); ?></span>
          </summary>
          <div class="app-details-card-container">
            <pre><code><?php e($metadataPretty); ?></code></pre>
          </div>
        </details>
      <?php endif; ?>
    </div>
  </section>

  <aside id="app-details-aside-section">
    <div class="app-details-aside-section">
      <hgroup>
        <strong><?php e(t('System audit')); ?></strong>
        <p><small><?php e($requestId !== '' ? $requestId : '-'); ?></small></p>
      </hgroup>
      <hr>
      <div class="grid">
        <div>
          <small><?php e(t('ID')); ?></small>
          <p>
            <span class="badge" data-variant="neutral" data-copy="true" data-copy-value="<?php e((string) ($auditLog['id'] ?? '')); ?>">
              <?php e((string) ($auditLog['id'] ?? '-')); ?>
            </span>
          </p>
        </div>
        <div>
          <small><?php e(t('Created')); ?></small>
          <p><?php e(dt((string) ($auditLog['created_at'] ?? '')) ?: '-'); ?></p>
        </div>
      </div>
      <div>
        <small><?php e(t('Request ID')); ?></small>
        <p>
          <span class="badge" data-variant="neutral" data-copy="true" data-copy-value="<?php e($requestId); ?>">
            <?php e($requestIdDisplay); ?>
          </span>
        </p>
      </div>
      <div>
        <small><?php e(t('IP hash')); ?></small>
        <p>
          <span class="badge" data-variant="neutral" data-copy="true" data-copy-value="<?php e($ipHash); ?>">
            <?php e($ipHashDisplay); ?>
          </span>
        </p>
      </div>
      <div>
        <small><?php e(t('User agent hash')); ?></small>
        <p>
          <span class="badge" data-variant="neutral" data-copy="true" data-copy-value="<?php e($userAgentHash); ?>">
            <?php e($userAgentHashDisplay); ?>
          </span>
        </p>
      </div>
    </div>
  </aside>
</div>
