<?php
/**
 * Widget: An- & Abmeldungen
 * ---------------------------------------------------------------
 * Tabelle: recipients
 *   • eintrag_datum   (DATETIME) – Zeitpunkt der Anmeldung
 *   • status          (ENUM 'active','unsubscribed', …)
 */

try {
    /* -- 1) heute angemeldet ----------------------------------- */
    $signedToday = (int)$db->query("
        SELECT COUNT(*)
        FROM recipients
        WHERE DATE(created_at) = CURDATE()
    ")->fetchColumn();

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

    /* -- 2) gesamt aktiv --------------------------------------- */
    $activeTotal = (int)$db->query("
        SELECT COUNT(*)
        FROM recipients
        WHERE status = 'active'
    ")->fetchColumn();

    ErrorLogger::logSqlSuccess(__FILE__ . ' | COUNT active total');

    /* -- 3) gesamt abgemeldet ---------------------------------- */
    $unsubTotal = (int)$db->query("
        SELECT COUNT(*)
        FROM recipients
        WHERE status = 'unsubscribed'
    ")->fetchColumn();

    ErrorLogger::logSqlSuccess(__FILE__ . ' | COUNT unsubscribed total');

} 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 An-/Abmelde-Statistiken konnten nicht geladen werden.
          </div>";
    return;
}
?>

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

    <i class="ti ti-user-plus fs-1 mb-2"></i>

    <!-- Heute angemeldet -->
    <h6 class="text-muted mb-1">heute angemeldet</h6>
    <p class="display-6 fw-bold mb-3">
        <?= number_format($signedToday,0,',','.') ?>
    </p>

    <div class="row">
      <div class="col-6 border-end">
        <h6 class="text-muted mb-1">aktiv</h6>
        <p class="fs-4 fw-semibold mb-0">
            <?= number_format($activeTotal,0,',','.') ?>
        </p>
      </div>
      <div class="col-6">
        <h6 class="text-muted mb-1">abgemeldet</h6>
        <p class="fs-4 fw-semibold mb-0">
            <?= number_format($unsubTotal,0,',','.') ?>
        </p>
      </div>
    </div>

  </div>
</div>
