<?php
use MintyPHP\Domain\Taxonomy\MailLogStatus;

/**
 * @var array $mailLog
 */

$mailLog = $mailLog ?? [];
$status = MailLogStatus::normalizeOr((string) ($mailLog['status'] ?? ''), MailLogStatus::Queued);
$statusBadge = $status->badgeVariant();

?>

<div class="app-details-container">
  <section>
    <?php
    $breadcrumbs = [
      ['label' => t('Home'), 'path' => 'admin'],
      ['label' => t('Mail logs'), 'path' => 'admin/mail-log'],
      ['label' => t('View')],
    ];
    require templatePath('partials/app-breadcrumb.phtml');
    ?>
    <div class="app-details-titlebar">
      <h1>
        <a href="admin/mail-log" title="<?php e(t('Back')); ?>"><i class="bi bi-arrow-left"></i></a>
        <?php e(t('View mail log')); ?>
      </h1>
    </div>

    <div class="app-details-content">
      <details open>
        <summary><?php e(t('Email details')); ?></summary>
        <hr>
        <div class="app-details-section">
          <div class="grid">
            <div class="app-form-group">
              <label><?php e(t('Recipient')); ?></label>
              <input type="text" value="<?php e($mailLog['to_email'] ?? ''); ?>" readonly>
            </div>
            <div class="app-form-group">
              <label><?php e(t('Subject')); ?></label>
              <input type="text" value="<?php e($mailLog['subject'] ?? ''); ?>" readonly>
            </div>

          </div>
          <div class="app-form-group">
            <label><?php e(t('Template')); ?></label>
            <input type="text" value="<?php e($mailLog['template'] ?? '-'); ?>" readonly>
          </div>
        </div>
      </details>

      <?php if ($status === MailLogStatus::Sent): ?>
        <hr>
        <details open>
          <summary><?php e(t('Delivery information')); ?></summary>
          <hr>
          <div class="app-details-section">
            <div class="app-form-group">
              <label><?php e(t('Sent at')); ?></label>
              <input type="text" value="<?php e(dt($mailLog['sent_at'] ?? '')); ?>" readonly>
            </div>
            <?php if (!empty($mailLog['provider_message_id'])): ?>
              <div class="app-form-group">
                <label><?php e(t('Provider message ID')); ?></label>
                <input type="text" value="<?php e($mailLog['provider_message_id'] ?? ''); ?>" readonly>
              </div>
            <?php endif; ?>
          </div>
        </details>
      <?php endif; ?>

      <?php if ($status === MailLogStatus::Failed && !empty($mailLog['error_message'])): ?>
        <hr>
        <details open>
          <summary><?php e(t('Error information')); ?></summary>
          <hr>
          <div class="app-details-section">
            <div class="app-form-group">
              <label><?php e(t('Error message')); ?></label>
              <textarea readonly rows="6"><?php e($mailLog['error_message'] ?? ''); ?></textarea>
            </div>
          </div>
        </details>
      <?php endif; ?>
    </div>
  </section>
  <aside id="app-details-aside-section">
    <div class="app-details-aside-section">
      <hgroup>
        <strong><?php e($mailLog['to_email'] ?? ''); ?></strong>
        <p><small><?php e(t('Mail log')); ?></small></p>
      </hgroup>
      <hr>
      <div class="grid">
        <div>
          <small><?php e(t('ID')); ?></small>
          <p>
            <span class="badge" data-variant="neutral">
              <?php e($mailLog['id'] ?? '-'); ?>
            </span>
          </p>
        </div>
        <div>
          <small>
            <?php e(t('Status')); ?>
          </small>
          <div>
            <span class="badge" data-variant="<?php e($statusBadge); ?>">
              <?php e(t($status->labelToken())); ?>
            </span>
          </div>
        </div>
      </div>
      <div>
        <small><?php e(t('Created')); ?></small>
        <p><?php e(dt($mailLog['created_at'] ?? '') ?: '-'); ?></p>
      </div>
      <?php if ($status === MailLogStatus::Sent && !empty($mailLog['sent_at'])): ?>
        <div>
          <small><?php e(t('Sent')); ?></small>
          <p><?php e(dt($mailLog['sent_at'] ?? '') ?: '-'); ?></p>
        </div>
      <?php endif; ?>
      <?php if (!empty($mailLog['provider_message_id'])): ?>
        <div class="grid">
          <div>
            <small><?php e(t('Provider message ID')); ?></small>
            <span class="badge" data-copy="true" data-copy-value="<?php e($mailLog['provider_message_id']); ?>"
              data-variant="neutral"><?php e(substr($mailLog['provider_message_id'], 0, 20)); ?></span>
          </div>
        </div>
      <?php endif; ?>
    </div>
  </aside>
</div>
