<section class="cards three">
    <div class="card stat"><h3>Twilio</h3><p><?= $providerStatus['twilio']['configured'] ? 'Configured' : 'Missing' ?></p><span><?= e($providerStatus['twilio']['from']) ?></span></div>
    <div class="card stat"><h3>SMTP</h3><p><?= $providerStatus['smtp']['configured'] ? 'Configured' : 'Missing' ?></p><span><?= e($providerStatus['smtp']['from']) ?></span></div>
    <div class="card stat"><h3>IMAP</h3><p><?= $providerStatus['imap']['configured'] ? 'Configured' : 'Missing' ?></p><span><?= e($providerStatus['imap']['mailbox']) ?></span></div>
</section>

<section class="grid two">
    <div class="card">
        <div class="toolbar"><h2>Provider Configuration Details</h2><span class="badge">Operations</span></div>
        <table>
            <tbody>
                <tr><th>Twilio SID</th><td><?= e(mask_secret($providerDetails['twilio']['sid'] ?? '')) ?></td></tr>
                <tr><th>Twilio From</th><td><?= e($providerDetails['twilio']['from_number'] ?? '') ?></td></tr>
                <tr><th>SMTP Host</th><td><?= e($providerDetails['smtp']['smtp_host'] ?? '') ?>:<?= (int)($providerDetails['smtp']['smtp_port'] ?? 0) ?></td></tr>
                <tr><th>SMTP Security</th><td><?= e($providerDetails['smtp']['smtp_secure'] ?? '') ?></td></tr>
                <tr><th>Mail From</th><td><?= e($providerDetails['smtp']['from_email'] ?? '') ?></td></tr>
                <tr><th>IMAP Host</th><td><?= e($providerDetails['imap']['host'] ?? '') ?>:<?= (int)($providerDetails['imap']['port'] ?? 0) ?></td></tr>
                <tr><th>IMAP Flags</th><td><?= e($providerDetails['imap']['flags'] ?? '') ?></td></tr>
                <tr><th>IMAP Mailbox</th><td><?= e($providerDetails['imap']['mailbox'] ?? '') ?></td></tr>
                <tr><th>Last Tested</th><td><?= e($lastTestedAt ?: 'Never') ?></td></tr>
            </tbody>
        </table>
    </div>
    <div class="card">
        <div class="toolbar"><h2>Live Connection Tests</h2><span class="badge">Live</span></div>
        <form method="post" action="index.php?route=settings/runProviderTests">
            <?= csrf_input() ?>
            <button class="btn primary">Run Provider Tests</button>
        </form>
        <p class="small muted" style="margin-top:12px;">These tests perform a live Twilio account request, SMTP handshake/auth check, and IMAP mailbox login.</p>
        <p class="small muted" style="margin-top:12px;">SendGrid certificate mismatch tip: if SMTP shows a host-name mismatch for <code>smtp.sendgrid.net</code>, switch the app to <strong>TLS</strong> on port <strong>587</strong> and verify the server resolves SendGrid correctly.</p>
        <table style="margin-top:16px;">
            <thead><tr><th>Provider</th><th>Status</th><th>Latency</th><th>Result</th></tr></thead>
            <tbody>
                <?php foreach (($testResults ?: ['twilio' => ['ok' => null, 'message' => 'Not run yet'], 'smtp' => ['ok' => null, 'message' => 'Not run yet'], 'imap' => ['ok' => null, 'message' => 'Not run yet']]) as $name => $result): ?>
                    <tr>
                        <td><?= e(strtoupper($name)) ?></td>
                        <td><?= $result['ok'] === true ? 'PASS' : ($result['ok'] === false ? 'FAIL' : '—') ?></td>
                        <td><?= isset($result['latency_ms']) ? (int)$result['latency_ms'] . ' ms' : '—' ?></td>
                        <td><?= e($result['message'] ?? '') ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>