<?php
/**
 * Widget: Mail-Statistiken
 * ------------------------------------------------------------------
 * Zeigt
 *   • Anzahl der im aktuellen Monat erfolgreich versendeten Mails (status = 'sent')
 *   • Anzahl der heute   erfolgreich versendeten Mails            (status = 'sent')
 *   • Aktuell offene Jobs in der Warteschlange                    (status IN pending|sending|processing)
 *
 * Tabelle: send_queue
 * Spalten: status (ENUM), sent_at (DATETIME)
 */

try {

    /* --------------------------------------------------------------
     * 1) Im aktuellen Monat versendete Mails
     * ------------------------------------------------------------ */
    $sqlSentMonth = "
        SELECT COUNT(*)
        FROM send_queue
        WHERE status = 'sent'
          AND YEAR(sent_at)  = YEAR(CURDATE())
          AND MONTH(sent_at) = MONTH(CURDATE())
    ";
    $sentMonth = (int)$db->query($sqlSentMonth)->fetchColumn();

    ErrorLogger::logSqlSuccess(__FILE__ . ' | COUNT sent this month from send_queue');

    /* --------------------------------------------------------------
     * 2) Heute versendete Mails
     * ------------------------------------------------------------ */
    $sqlSentToday = "
        SELECT COUNT(*)
        FROM send_queue
        WHERE status = 'sent'
          AND DATE(sent_at) = CURDATE()
    ";
    $sentToday = (int)$db->query($sqlSentToday)->fetchColumn();

    ErrorLogger::logSqlSuccess(__FILE__ . ' | COUNT sent today from send_queue');

    /* --------------------------------------------------------------
     * 3) Offene Jobs in der Queue
     * ------------------------------------------------------------ */
    $sqlOpen = "
        SELECT COUNT(*)
        FROM send_queue
        WHERE status IN ('pending', 'processing')
    ";
    $openJobs = (int)$db->query($sqlOpen)->fetchColumn();

    ErrorLogger::logSqlSuccess(__FILE__ . ' | COUNT open jobs from send_queue');

} catch (Throwable $e) {
    // ➜ Bei Fehler: Log & Hinweis anzeigen (auto-dismiss)
    ErrorLogger::logSqlError(__FILE__ . ' | ' . $e->getMessage());

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

<!-- --------------------------------------------------------------
     HTML-Ausgabe – schlanke Kennzahlenkarte
----------------------------------------------------------------- -->
<div class="card shadow-sm h-100">
  <div class="card-body text-center">

    <!-- Haupt-Icon -->
    <i class="ti ti-mail-fast fs-1 mb-3"></i>


  

    <!-- Mails heute -->
    <h6 class="text-muted mb-1">heute versendet</h6>
    <p class="fs-4 fw-semibold mb-2">
        <?= number_format($sentToday, 0, ',', '.') ?>
    </p>

    <hr class="my-2">

    <!-- Offene Jobs -->
    <h6 class="text-muted mb-1">offen in&nbsp;Queue</h6>
    <p class="fs-4 fw-semibold mb-0">
        <?= number_format($openJobs, 0, ',', '.') ?>
    </p>
  <hr class="my-2">
    <!-- Mails diesen Monat -->
    <h6 class="text-muted mb-1">diesen&nbsp;Monat versendet</h6>
    <p class="display-6 fw-bold mb-2">
        <?= number_format($sentMonth, 0, ',', '.') ?>
    </p>    

  </div>
</div>
