<section class="cards four">
    <div class="card stat"><h3>App Version</h3><p>V<?= e($appVersion) ?></p><span>Runtime code version • source: <?= e($appVersionSource ?? 'unknown') ?></span></div>
    <div class="card stat"><h3>Schema Version</h3><p><?= e($schemaVersion) ?></p><span><?= $schemaMatches ? 'Aligned' : 'Mismatch' ?></span></div>
    <div class="card stat"><h3>Worker</h3><p><?= e($heartbeat['label']) ?></p><span><?= e($heartbeat['at'] ?? 'No heartbeat') ?></span></div>
    <div class="card stat"><h3>Dead Letters</h3><p><?= (int)($queueStats['dead_letter_jobs'] ?? 0) ?></p><span>Requeue from this page</span></div>
</section>

<?php if (!$schemaMatches): ?>
<section class="card">
    <div class="toolbar"><h2>Version Mismatch Tools</h2><span class="badge">Repair</span></div>
    <p class="alert error">Code and database schema versions do not match.</p>
    <p style="margin-top:12px;display:flex;gap:10px;flex-wrap:wrap;">
        <a class="btn secondary" href="install.php?action=repair">Run Repair / Migrations</a>
        <form method="post" action="index.php?route=settings/syncSchemaVersion" style="display:inline;">
            <?= csrf_input() ?>
            <button class="btn primary" type="submit">Sync schema version to <?= e($appVersion) ?></button>
        </form>
    </p>
    <p style="margin-top:8px;color:#64748b;font-size:14px;">Use sync only when you already deployed the full code update and only the recorded schema version is stale.</p>
</section>
<?php endif; ?>

<?php if ($missingTables): ?>
<section class="card">
    <h2>Missing Tables Detected</h2>
    <p class="alert error">The following required tables are missing: <?= e(implode(', ', $missingTables)) ?>.</p>
    <p><a class="btn primary" href="install.php?action=repair">Run Repair</a></p>
</section>
<?php endif; ?>

<section class="grid two">
    <div class="card">
        <h2>Queue Status</h2>
        <table>
            <tbody>
                <tr><th>Queued</th><td><?= (int)($queueStats['queued_jobs'] ?? 0) ?></td></tr>
                <tr><th>Processing</th><td><?= (int)($queueStats['processing_jobs'] ?? 0) ?></td></tr>
                <tr><th>Done</th><td><?= (int)($queueStats['done_jobs'] ?? 0) ?></td></tr>
                <tr><th>Failed</th><td><?= (int)($queueStats['failed_jobs'] ?? 0) ?></td></tr>
                <tr><th>Dead Letters</th><td><?= (int)($queueStats['dead_letter_jobs'] ?? 0) ?></td></tr>
            </tbody>
        </table>
    </div>
    <div class="card">
        <h2>Recent Queue Jobs</h2>
        <table>
            <thead><tr><th>ID</th><th>Type</th><th>Status</th><th>Attempts</th><th>Updated</th></tr></thead>
            <tbody>
            <?php foreach ($queueJobs as $job): ?>
                <tr>
                    <td>#<?= (int)$job['id'] ?></td>
                    <td><?= e($job['job_type']) ?></td>
                    <td><?= e($job['status']) ?></td>
                    <td><?= (int)$job['attempts'] ?></td>
                    <td><?= e($job['updated_at']) ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

<section class="card">
    <div class="toolbar"><h2>Dead Letter Replay</h2><span class="badge">Operations</span></div>
    <table>
        <thead><tr><th>ID</th><th>Source Job</th><th>Type</th><th>Error</th><th>Attempts</th><th>Created</th><th></th></tr></thead>
        <tbody>
        <?php foreach ($deadLetters as $dead): ?>
            <tr>
                <td>#<?= (int)$dead['id'] ?></td>
                <td>#<?= (int)$dead['queue_job_id'] ?></td>
                <td><?= e($dead['job_type']) ?></td>
                <td><?= e(substr((string)$dead['last_error'], 0, 120)) ?></td>
                <td><?= (int)$dead['attempts'] ?></td>
                <td><?= e($dead['created_at']) ?></td>
                <td><a class="btn secondary small" href="index.php?route=settings/replayDeadLetter&id=<?= (int)$dead['id'] ?>">Replay</a></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</section>

<section class="grid two">
    <div class="card">
        <h2>Audit Log</h2>
        <table>
            <thead><tr><th>When</th><th>User</th><th>Action</th><th>Entity</th></tr></thead>
            <tbody>
            <?php foreach ($auditLogs as $log): ?>
                <tr>
                    <td><?= e($log['created_at']) ?></td>
                    <td><?= e($log['user_name'] ?: 'System') ?></td>
                    <td><?= e($log['action']) ?></td>
                    <td><?= e($log['entity_type']) ?> #<?= (int)$log['entity_id'] ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <div class="card">
        <h2>Recent Messages</h2>
        <table>
            <thead><tr><th>When</th><th>Channel</th><th>Status</th><th>To</th></tr></thead>
            <tbody>
            <?php foreach ($recentMessages as $message): ?>
                <tr>
                    <td><?= e($message['created_at']) ?></td>
                    <td><?= e(strtoupper($message['channel'])) ?></td>
                    <td><?= e($message['status']) ?></td>
                    <td><?= e($message['to_address'] ?: $message['from_address']) ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

<section class="grid two">
    <div class="card">
        <h2>Failed Queue Retry Center</h2>
        <table><thead><tr><th>ID</th><th>Type</th><th>Error</th><th></th></tr></thead><tbody><?php foreach ($failedQueueJobs as $job): ?><tr><td>#<?= (int)$job['id'] ?></td><td><?= e($job['job_type']) ?></td><td><?= e(substr((string)$job['last_error'],0,90)) ?></td><td><a class="btn secondary small" href="index.php?route=settings/retryQueueJob&id=<?= (int)$job['id'] ?>">Retry</a></td></tr><?php endforeach; ?></tbody></table>
    </div>
    <div class="card">
        <h2>Recent Login History</h2>
        <table><thead><tr><th>When</th><th>User</th><th>Status</th><th>IP</th></tr></thead><tbody><?php foreach ($loginHistory as $row): ?><tr><td><?= e($row['created_at']) ?></td><td><?= e($row['user_name'] ?: $row['email']) ?></td><td><?= e($row['status']) ?></td><td><?= e($row['ip_address']) ?></td></tr><?php endforeach; ?></tbody></table>
    </div>
</section>


<section class="grid two">
    <div class="card">
        <div class="toolbar"><h2>Webhook Event Observatory</h2><span class="badge"><?= (int)($webhookSummary['total_count'] ?? 0) ?> events</span></div>
        <table>
            <tbody>
                <tr><th>Total Events</th><td><?= (int)($webhookSummary['total_count'] ?? 0) ?></td></tr>
                <tr><th>Verified</th><td><?= (int)($webhookSummary['verified_count'] ?? 0) ?></td></tr>
                <tr><th>Replayed</th><td><?= (int)($webhookSummary['replayed_count'] ?? 0) ?></td></tr>
                <tr><th>Last Event</th><td><?= e($webhookSummary['last_event_at'] ?? 'Never') ?></td></tr>
            </tbody>
        </table>
    </div>
    <div class="card">
        <div class="toolbar"><h2>Recent Webhook Logs</h2><span class="badge">Deliverability</span></div>
        <table>
            <thead><tr><th>Provider</th><th>Event</th><th>Signature</th><th>Replay</th><th></th></tr></thead>
            <tbody>
            <?php foreach (($webhookLogs ?? []) as $log): ?>
                <tr>
                    <td><?= e($log['provider']) ?></td>
                    <td><?= e($log['event_type']) ?></td>
                    <td><?= e($log['signature_status']) ?></td>
                    <td><?= e($log['replay_status']) ?></td>
                    <td><a class="btn secondary small" href="index.php?route=settings/replayWebhookEvent&id=<?= (int)$log['id'] ?>">Replay</a></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($webhookLogs)): ?><tr><td colspan="5" class="muted">No webhook logs captured yet.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</section>
