<?php
use MintyPHP\Domain\Taxonomy\UserLifecycleAction;
use MintyPHP\Domain\Taxonomy\UserLifecycleStatus;
use MintyPHP\Domain\Taxonomy\UserLifecycleTriggerType;

/**
 * @var array $auditLog
 * @var ?array $snapshot
 */

use MintyPHP\Session;

$auditLog = $auditLog ?? [];
$snapshot = is_array($snapshot ?? null) ? $snapshot : null;
$status = UserLifecycleStatus::normalizeOr((string) ($auditLog['status'] ?? ''), UserLifecycleStatus::Failed);
$statusVariant = $status->badgeVariant();

$action = UserLifecycleAction::normalizeOr((string) ($auditLog['action'] ?? ''), UserLifecycleAction::Deactivate);
$triggerType = UserLifecycleTriggerType::normalizeOr((string) ($auditLog['trigger_type'] ?? ''), UserLifecycleTriggerType::System);
$pageAuth = is_array($viewAuth['page'] ?? null) ? $viewAuth['page'] : [];
$canRestore = (bool) ($pageAuth['can_restore'] ?? false);
$isRestorable = $canRestore
  && $action === UserLifecycleAction::Delete
  && $status === UserLifecycleStatus::Success
  && empty($auditLog['restored_at'])
  && !empty($auditLog['snapshot_enc']);
?>

<div class="app-details-container">
  <section>
    <?php
    $breadcrumbs = [
      ['label' => t('Home'), 'path' => 'admin'],
      ['label' => t('User lifecycle logs'), 'path' => 'admin/user-lifecycle-audit'],
      ['label' => t('View')],
    ];
    require templatePath('partials/app-breadcrumb.phtml');

    $titlebar = [
      'title' => t('View user lifecycle audit entry'),
      'backHref' => 'admin/user-lifecycle-audit',
      'backTitle' => t('Back'),
    ];
    require templatePath('partials/app-details-titlebar.phtml');
    ?>

    <div class="app-details-content">
      <details open>
        <summary><?php e(t('Lifecycle event')); ?></summary>
        <hr>
        <div class="grid">
          <div>
            <small><?php e(t('Created')); ?></small>
            <p><?php e(dt((string) ($auditLog['created_at'] ?? '')) ?: '-'); ?></p>
          </div>
          <div>
            <small><?php e(t('Status')); ?></small>
            <p><span class="badge" data-variant="<?php e($statusVariant); ?>"><?php e(t($status->labelToken())); ?></span></p>
          </div>
        </div>
        <div class="grid">
          <div>
            <small><?php e(t('Action')); ?></small>
            <p><?php e(t($action->labelToken())); ?></p>
          </div>
          <div>
            <small><?php e(t('Trigger')); ?></small>
            <p><?php e(t($triggerType->labelToken())); ?></p>
          </div>
        </div>
        <div class="grid">
          <div>
            <small><?php e(t('Reason code')); ?></small>
            <p><?php e((string) ($auditLog['reason_code'] ?? '') !== '' ? (string) $auditLog['reason_code'] : '-'); ?></p>
          </div>
          <div>
            <small><?php e(t('Run UUID')); ?></small>
            <p><?php e((string) ($auditLog['run_uuid'] ?? '') !== '' ? (string) $auditLog['run_uuid'] : '-'); ?></p>
          </div>
        </div>
      </details>

      <hr>
      <details open>
        <summary><?php e(t('Target')); ?></summary>
        <hr>
        <div class="grid">
          <div>
            <small><?php e(t('Target UUID')); ?></small>
            <p><?php e((string) ($auditLog['target_user_uuid'] ?? '') !== '' ? (string) $auditLog['target_user_uuid'] : '-'); ?></p>
          </div>
          <div>
            <small><?php e(t('Target email')); ?></small>
            <p><?php e((string) ($auditLog['target_user_email'] ?? '') !== '' ? (string) $auditLog['target_user_email'] : '-'); ?></p>
          </div>
        </div>
      </details>

      <hr>
      <details>
        <summary><?php e(t('Policy')); ?></summary>
        <hr>
        <div class="grid">
          <div>
            <small><?php e(t('Deactivate users after inactivity (days)')); ?></small>
            <p><?php e((string) ((int) ($auditLog['policy_deactivate_days'] ?? 0))); ?></p>
          </div>
          <div>
            <small><?php e(t('Delete inactive users after (days)')); ?></small>
            <p><?php e((string) ((int) ($auditLog['policy_delete_days'] ?? 0))); ?></p>
          </div>
        </div>
      </details>

      <?php if ($snapshot !== null): ?>
        <hr>
        <details>
          <summary><?php e(t('Snapshot summary')); ?></summary>
          <hr>
          <div class="grid">
            <div>
              <small><?php e(t('First name')); ?></small>
              <p><?php e((string) ($snapshot['first_name'] ?? '') !== '' ? (string) $snapshot['first_name'] : '-'); ?></p>
            </div>
            <div>
              <small><?php e(t('Last name')); ?></small>
              <p><?php e((string) ($snapshot['last_name'] ?? '') !== '' ? (string) $snapshot['last_name'] : '-'); ?></p>
            </div>
          </div>
          <div class="grid">
            <div>
              <small><?php e(t('Email')); ?></small>
              <p><?php e((string) ($snapshot['email'] ?? '') !== '' ? (string) $snapshot['email'] : '-'); ?></p>
            </div>
            <div>
              <small><?php e(t('Locale')); ?></small>
              <p><?php e((string) ($snapshot['locale'] ?? '') !== '' ? (string) $snapshot['locale'] : '-'); ?></p>
            </div>
          </div>
          <div class="grid">
            <div>
              <small><?php e(t('Theme')); ?></small>
              <p><?php e((string) ($snapshot['theme'] ?? '') !== '' ? (string) $snapshot['theme'] : '-'); ?></p>
            </div>
            <div>
              <small><?php e(t('Snapshot version')); ?></small>
              <p><?php e((string) ((int) ($auditLog['snapshot_version'] ?? 1))); ?></p>
            </div>
          </div>
        </details>
      <?php endif; ?>

      <hr>
      <details open>
        <summary><?php e(t('Restore status')); ?></summary>
        <hr>
        <div class="grid">
          <div>
            <small><?php e(t('Restored at')); ?></small>
            <p><?php e(dt((string) ($auditLog['restored_at'] ?? '')) ?: '-'); ?></p>
          </div>
          <div>
            <small><?php e(t('Restored by')); ?></small>
            <p><?php e((string) ($auditLog['restored_by_user_display_name'] ?? '') !== '' ? (string) $auditLog['restored_by_user_display_name'] : '-'); ?></p>
          </div>
        </div>
        <div>
          <small><?php e(t('Restored user')); ?></small>
          <p>
            <?php if (!empty($auditLog['restored_user_uuid'])): ?>
              <a href="admin/users/edit/<?php e((string) $auditLog['restored_user_uuid']); ?>">
                <?php e((string) ($auditLog['restored_user_display_name'] ?? $auditLog['restored_user_uuid'])); ?>
              </a>
            <?php else: ?>
              -
            <?php endif; ?>
          </p>
        </div>
        <?php if ($isRestorable): ?>
          <hr>
          <form method="post" action="admin/user-lifecycle-audit/restore/<?php e((string) ($auditLog['id'] ?? 0)); ?>">
            <?php Session::getCsrfInput(); ?>
            <button type="submit" class="warning" data-confirm-message="<?php e(t('Restore this user from lifecycle snapshot?')); ?>">
              <?php e(t('Restore user')); ?>
            </button>
          </form>
        <?php endif; ?>
      </details>
    </div>
  </section>

  <aside id="app-details-aside-section">
    <div class="app-details-aside-section">
      <hgroup>
        <strong><?php e(t('User lifecycle logs')); ?></strong>
        <p><small><?php e((string) ($auditLog['run_uuid'] ?? '') !== '' ? (string) $auditLog['run_uuid'] : '-'); ?></small></p>
      </hgroup>
      <hr>
      <div>
        <small><?php e(t('ID')); ?></small>
        <p><span class="badge" data-variant="neutral"><?php e((string) ($auditLog['id'] ?? '-')); ?></span></p>
      </div>
      <div>
        <small><?php e(t('Status')); ?></small>
        <p><span class="badge" data-variant="<?php e($statusVariant); ?>"><?php e(t($status->labelToken())); ?></span></p>
      </div>
      <div>
        <small><?php e(t('Action')); ?></small>
        <p><?php e(t($action->labelToken())); ?></p>
      </div>
    </div>
  </aside>
</div>
