<?php
// admin/status_monitor_api.php

session_start();
require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/../auth/AuthController.php';

$auth = new AuthController();
if (!$auth->isLoggedIn() || ($_SESSION['admin_role'] ?? '') !== 'admin') {
    http_response_code(403);
    exit('Zugriff verweigert.');
}

$db = Database::getInstance();
$statusCounts = [];
$sqlStatus = 'SELECT status, COUNT(*) AS count FROM send_queue GROUP BY status';

try {
    $statusCounts = $db->query($sqlStatus)->fetchAll(PDO::FETCH_KEY_PAIR);
    ErrorLogger::logSqlSuccess('status_monitor_api: load queue status counts | SQL: ' . $sqlStatus . ' | params: []');
} catch (Throwable $e) {
    ErrorLogger::logSqlError('status_monitor_api: load queue status counts failed | SQL: ' . $sqlStatus . ' | params: [] | error: ' . $e->getMessage());
    $statusCounts = [];
}

$total = array_sum($statusCounts);

?>
<ul class="list-unstyled">
  <li><strong>Gesamt:</strong> <?= $total ?></li>
  <li><strong>Ausstehend:</strong> <?= $statusCounts['pending'] ?? 0 ?></li>
  <li><strong>Gesendet:</strong> <?= $statusCounts['sent'] ?? 0 ?></li>
  <li><strong>Fehlgeschlagen:</strong> <?= $statusCounts['failed'] ?? 0 ?></li>
</ul>
