<?php
function nd_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$logs = $logs ?? [];
$stats = $stats ?? [];
$enabledPrefs = $enabledPrefs ?? [];
?>

<div class="container-fluid py-3">

    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#1d4ed8);color:#fff;">
        <div class="card-body p-4">
            <h3 class="fw-bold mb-1"><i class="fa fa-newspaper"></i> Notification Digest Monitor</h3>
            <div class="opacity-75">Monitor unified daily/weekly digest delivery for this conference.</div>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <?php foreach (['sent','failed','skipped'] as $st): ?>
            <div class="col-md-4">
                <div class="card border-0 shadow-sm" style="border-radius:20px;">
                    <div class="card-body text-center">
                        <div class="fs-2 fw-bold"><?= (int)($stats[$st] ?? 0) ?></div>
                        <div class="text-muted"><?= nd_h($st) ?></div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold mb-3">Enabled Digest Preferences</h5>

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>Notification Type</th>
                        <th>Frequency</th>
                        <th>Users Count</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($enabledPrefs as $p): ?>
                        <tr>
                            <td><?= nd_h($p['notification_type']) ?></td>
                            <td><span class="badge bg-primary"><?= nd_h($p['digest_frequency']) ?></span></td>
                            <td><?= (int)$p['users_count'] ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (!$enabledPrefs): ?>
                        <tr><td colspan="3" class="text-center text-muted py-4">No digest preferences enabled yet.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>

            <div class="alert alert-info mt-3">
                CLI test for unified digest:
                <code>php /var/www/html/easychair/tools/notification_digest_worker.php --frequency=daily --conference_id=<?= (int)($_SESSION['conference_id'] ?? 0) ?> --dry-run</code>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold mb-3">Digest Delivery Logs</h5>

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>ID</th>
                        <th>User</th>
                        <th>Frequency</th>
                        <th>Count</th>
                        <th>Recipient</th>
                        <th>Status</th>
                        <th>Sent</th>
                        <th>Error</th>
                        <th>Preview</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($logs as $l): ?>
                        <tr>
                            <td><?= (int)$l['id'] ?></td>
                            <td>
                                <?= nd_h(trim($l['user_name'] ?? '') ?: ($l['email'] ?? '')) ?>
                                <div class="text-muted small"><?= nd_h($l['email'] ?? '') ?></div>
                            </td>
                            <td><span class="badge bg-primary"><?= nd_h($l['digest_frequency']) ?></span></td>
                            <td><?= (int)$l['notifications_count'] ?></td>
                            <td><?= nd_h($l['recipient_email']) ?></td>
                            <td>
                                <span class="badge <?= ($l['status'] ?? '') === 'sent' ? 'bg-success' : (($l['status'] ?? '') === 'failed' ? 'bg-danger' : 'bg-secondary') ?>">
                                    <?= nd_h($l['status']) ?>
                                </span>
                            </td>
                            <td><?= nd_h($l['sent_at']) ?></td>
                            <td class="small text-danger"><?= nd_h(mb_substr((string)($l['error_message'] ?? ''), 0, 180)) ?></td>
                            <td>
                                <a target="_blank" class="btn btn-sm btn-outline-secondary" href="/easychair/admin/notification-digest/preview?id=<?= (int)$l['id'] ?>">
                                    Preview
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$logs): ?>
                        <tr><td colspan="9" class="text-center text-muted py-4">No digest logs yet.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>

        </div>
    </div>
</div>
