<?php
$stats = $connectedMailboxStats ?? ['total_count' => 0, 'renewal_due_count' => 0, 'error_count' => 0, 'last_synced_at' => null];
$queueStats = $queueStats ?? ['queued_jobs' => 0, 'processing_jobs' => 0, 'failed_jobs' => 0];
$mailboxes = $connectedMailboxes ?? [];
$subscriptionsDue = $mailboxSubscriptionsDue ?? [];
$queueJobs = $queueJobs ?? [];
?>
<section class="workspace-hero">
    <div>
        <h2>Sync Health</h2>
        <p>Monitor mailbox renewals, worker heartbeat, webhook subscription expiry, and queue activity so unified mailbox sync stays reliable.</p>
    </div>
    <div class="workspace-actions">
        <a class="btn secondary small" href="index.php?route=messages/connectedMailboxes">Connected Mailboxes</a>
        <a class="btn secondary small" href="index.php?route=settings/diagnostics">Diagnostics</a>
    </div>
</section>

<section class="cards four compact-stats">
    <div class="card stat"><h3>Mailboxes</h3><p><?= (int)($stats['total_count'] ?? 0) ?></p><span><?= (int)($stats['connected_count'] ?? 0) ?> connected</span></div>
    <div class="card stat alt"><h3>Renewals due</h3><p><?= (int)($stats['renewal_due_count'] ?? 0) ?></p><span><?= count($subscriptionsDue) ?> expiring subscriptions</span></div>
    <div class="card stat alt"><h3>Queue backlog</h3><p><?= (int)($queueStats['queued_jobs'] ?? 0) ?></p><span><?= (int)($queueStats['processing_jobs'] ?? 0) ?> processing · <?= (int)($queueStats['failed_jobs'] ?? 0) ?> failed</span></div>
    <div class="card stat alt"><h3>Heartbeat</h3><p><?= e($mailboxSyncLastHeartbeat ?: 'never') ?></p><span>Worker heartbeat <?= e($workerLastHeartbeat ?: 'never') ?></span></div>
</section>

<section class="grid two">
    <div class="card">
        <div class="toolbar"><h2>Mailbox sync status</h2><span class="badge"><?= count($mailboxes) ?></span></div>
        <table>
            <thead><tr><th>Mailbox</th><th>Sync</th><th>OAuth</th><th>Subscription</th><th>Last sync</th><th>Action</th></tr></thead>
            <tbody>
            <?php foreach ($mailboxes as $mailbox): ?>
                <tr>
                    <td><strong><?= e($mailbox['name'] ?? '') ?></strong><br><span class="small muted"><?= e($mailbox['email_address'] ?? '') ?></span></td>
                    <td><span class="badge <?= e($mailbox['sync_state'] ?? 'idle') ?>"><?= e($mailbox['sync_state'] ?? 'idle') ?></span></td>
                    <td><span class="badge <?= e($mailbox['oauth_status'] ?? 'planned') ?>"><?= e($mailbox['oauth_status'] ?? 'planned') ?></span></td>
                    <td><?= e($mailbox['subscription_expires_at'] ?? '—') ?></td>
                    <td><?= e($mailbox['last_synced_at'] ?? 'never') ?></td>
                    <td>
                        <form method="post" action="index.php?route=messages/queueMailboxSync" style="display:inline-flex;gap:8px;">
                            <?= csrf_input() ?>
                            <input type="hidden" name="id" value="<?= (int)$mailbox['id'] ?>">
                            <button class="btn secondary small">Queue sync</button>
                        </form>
                    </td>
                </tr>
            <?php endforeach; ?>
            <?php if (!$mailboxes): ?><tr><td colspan="6" class="muted">No mailbox records yet.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
    <div class="card">
        <div class="toolbar"><h2>Renewals due now</h2><span class="badge"><?= count($subscriptionsDue) ?></span></div>
        <table>
            <thead><tr><th>Mailbox</th><th>Provider</th><th>Expires</th><th>Verification</th><th>Action</th></tr></thead>
            <tbody>
            <?php foreach ($subscriptionsDue as $subscription): ?>
                <tr>
                    <td><?= e(($subscription['mailbox_name'] ?? 'Mailbox') . ' · ' . ($subscription['email_address'] ?? '')) ?></td>
                    <td><?= e($subscription['provider'] ?? '') ?></td>
                    <td><?= e($subscription['expires_at'] ?? '') ?></td>
                    <td><?= e($subscription['verification_state'] ?? '') ?></td>
                    <td>
                        <form method="post" action="index.php?route=messages/renewMailboxSubscription">
                            <?= csrf_input() ?>
                            <input type="hidden" name="id" value="<?= (int)($subscription['connected_mailbox_id'] ?? 0) ?>">
                            <button class="btn secondary small">Renew</button>
                        </form>
                    </td>
                </tr>
            <?php endforeach; ?>
            <?php if (!$subscriptionsDue): ?><tr><td colspan="5" class="muted">No renewals due yet.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</section>

<section class="card">
    <div class="toolbar"><h2>Recent mailbox sync jobs</h2><span class="badge"><?= count($queueJobs) ?></span></div>
    <table>
        <thead><tr><th>Job type</th><th>Status</th><th>Available</th><th>Updated</th><th>Attempts</th><th>Last error</th></tr></thead>
        <tbody>
        <?php foreach ($queueJobs as $job): ?>
            <tr>
                <td><?= e($job['job_type'] ?? '') ?></td>
                <td><?= e($job['status'] ?? '') ?></td>
                <td><?= e($job['available_at'] ?? '') ?></td>
                <td><?= e($job['updated_at'] ?? '') ?></td>
                <td><?= (int)($job['attempts'] ?? 0) ?></td>
                <td class="small" style="max-width:320px;"><?= e($job['last_error'] ?? '') ?></td>
            </tr>
        <?php endforeach; ?>
        <?php if (!$queueJobs): ?><tr><td colspan="6" class="muted">No mailbox sync jobs found.</td></tr><?php endif; ?>
        </tbody>
    </table>
</section>
