<?php
/**
 * Widget: Queue-Status
 * ---------------------------------------------------------------
 * Zeigt für die aktuelle Versand-Warteschlange:
 *   • Anzahl der Jobs je Status (pending, sending, processing, error)
 *   • Älteste geplante Mail (Earliest scheduled_at) – nur informativ
 *
 * Tabelle: send_queue
 * Spalten: status (ENUM), scheduled_at (DATETIME)
 *
 * Logger:  ErrorLogger mit **einem** String-Parameter
 */

try {
    /* ============================================================
     * 1) Zählung pro Status
     * ========================================================== */
    $statuses = ['pending','processing','error'];
    $sql = "
        SELECT status, COUNT(*) AS cnt
        FROM send_queue
        WHERE status IN ('pending', 'processing', 'error')
        GROUP BY status
    ";
    $rows = $db->query($sql)->fetchAll(PDO::FETCH_KEY_PAIR);

    ErrorLogger::logSqlSuccess(__FILE__ . ' | GROUP BY status FROM send_queue');

    // Reihenfolge & fehlende Stati auffüllen
    $counts = $rows + array_fill_keys($statuses, 0);

    /* ============================================================
     * 2) Älteste geplante Mail (nur pending/sending/processing)
     * ========================================================== */
    $sqlOldest = "
        SELECT MIN(scheduled_at)
        FROM send_queue
        WHERE status IN ('pending', 'sent', 'processing')
    ";
    $oldest = $db->query($sqlOldest)->fetchColumn();

    ErrorLogger::logSqlSuccess(__FILE__ . ' | MIN scheduled_at FROM send_queue');

} 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>
        Der Queue-Status konnte nicht geladen werden.
    </div>";
    return;
}
?>

<!-- ============================================================
     3) HTML-Ausgabe (Bootstrap 5 + TML-Icons)
     ========================================================== -->
   
<div class="card shadow-sm h-100">
  <div class="card-body">

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

    <ul class="list-group list-group-flush small mb-3">
      <li class="list-group-item d-flex justify-content-between align-items-center px-0">
        <span><i class="ti ti-clock me-1"></i>pending</span>
        <span class="badge bg-secondary-subtle text-secondary fw-semibold">
          <?= number_format($counts['pending'], 0, ',', '.') ?>
        </span>
      </li>
      <li class="list-group-item d-flex justify-content-between align-items-center px-0">
        <span><i class="ti ti-progress me-1"></i>processing</span>
        <span class="badge bg-primary-subtle text-primary fw-semibold">
          <?= number_format($counts['processing'], 0, ',', '.') ?>
        </span>
      </li>
      <li class="list-group-item d-flex justify-content-between align-items-center px-0">
        <span><i class="ti ti-alert-circle me-1"></i>error</span>
        <span class="badge bg-danger-subtle text-danger fw-semibold">
          <?= number_format($counts['error'], 0, ',', '.') ?>
        </span>
      </li>
    </ul>



  </div>
</div>

