<?php
/**
 * Widget: Bounce-Statistik
 * ---------------------------------------------------------------
 * Tabelle: email_bounces
 * Spalten: bounce_type (ENUM), logged_at (DATETIME)
 */

try {
    /* ============================================================
     * 1) Heute gebouncte Mails
     * ========================================================== */
    $today = (int)$db->query("
        SELECT COUNT(*)
        FROM email_bounces
        WHERE DATE(logged_at) = CURDATE()
    ")->fetchColumn();

    ErrorLogger::logSqlSuccess(__FILE__ . ' | COUNT bounces today');

    /* ============================================================
     * 2) Gesamtzahl aller Bounces
     * ========================================================== */
    $total = (int)$db->query("
        SELECT COUNT(*) FROM email_bounces
    ")->fetchColumn();

    ErrorLogger::logSqlSuccess(__FILE__ . ' | COUNT all bounces');

    /* ============================================================
     * 3) Aufschlüsselung nach Bounce-Typ
     * ========================================================== */
    $rows = $db->query("
        SELECT bounce_type, COUNT(*) AS cnt
        FROM email_bounces
        GROUP BY bounce_type
    ")->fetchAll(PDO::FETCH_KEY_PAIR);

    ErrorLogger::logSqlSuccess(__FILE__ . ' | GROUP BY bounce_type');

    // Fehlende Typen mit 0 auffüllen
    $types  = ['hard','soft','spam','other'];
    $counts = $rows + array_fill_keys($types, 0);   // Reihenfolge vertauscht


} catch (Throwable $e) {
    ErrorLogger::logSqlError(__FILE__ . ' | ' . $e->getMessage());

    echo "<div class='alert alert-danger auto-dismiss' role='alert'>
            <i class='ti ti-bug me-1'></i>
            Die Bounce-Statistiken konnten nicht geladen werden.
          </div>";
    return;
}
?>

<div class="card shadow-sm h-100">
  <div class="card-body">

    <div class="d-flex align-items-center mb-3">
      <i class="ti ti-mail-x fs-3 me-2"></i>
      <h5 class="card-title mb-0">Bounces</h5>
    </div>

    <!-- Kennzahlen -->
    <div class="row text-center mb-3">
      <div class="col-6 border-end">
        <span class="fs-4 fw-bold"><?= number_format($today,0,',','.') ?></span><br>
        <small class="text-muted">heute</small>
      </div>
      <div class="col-6">
        <span class="fs-4 fw-bold"><?= number_format($total,0,',','.') ?></span><br>
        <small class="text-muted">gesamt</small>
      </div>
    </div>

    <!-- Typ-Liste -->
    <h6 class="text-muted fw-semibold small mb-2">
      <i class="ti ti-flag me-1"></i>nach Typ
    </h6>

    <ul class="list-group list-group-flush small">
      <li class="list-group-item d-flex justify-content-between align-items-center px-0">
        <span><i class="ti ti-shield-x me-1"></i>hard</span>
        <span class="badge bg-danger-subtle text-danger fw-semibold">
          <?= number_format($counts['hard'],0,',','.') ?>
        </span>
      </li>
      <li class="list-group-item d-flex justify-content-between align-items-center px-0">
        <span><i class="ti ti-shield me-1"></i>soft</span>
        <span class="badge bg-warning-subtle text-warning fw-semibold">
          <?= number_format($counts['soft'],0,',','.') ?>
        </span>
      </li>
      <li class="list-group-item d-flex justify-content-between align-items-center px-0">
        <span><i class="ti ti-mail-warning me-1"></i>spam</span>
        <span class="badge bg-info-subtle text-info fw-semibold">
          <?= number_format($counts['spam'],0,',','.') ?>
        </span>
      </li>
      <li class="list-group-item d-flex justify-content-between align-items-center px-0">
        <span><i class="ti ti-question-circle me-1"></i>other</span>
        <span class="badge bg-secondary-subtle text-secondary fw-semibold">
          <?= number_format($counts['other'],0,',','.') ?>
        </span>
      </li>
    </ul>

  </div>
</div>
