<?php
$sharedMailboxes = $sharedMailboxes ?? [];
$queueStats = $queueStats ?? [];
$users = $users ?? [];
?>
<section class="workspace-hero">
    <div>
        <h2>Shared Inboxes</h2>
        <p>Stand up team inboxes with shared ownership, saved views, mailbox rules, and service workflow expectations that work alongside Mail Center.</p>
    </div>
    <div class="workspace-actions">
        <a class="btn secondary small" href="index.php?route=messages/mailCenter">Open Mail Center</a>
        <a class="btn secondary small" href="index.php?route=messages/connectedMailboxes">Connected Mailboxes</a>
    </div>
</section>

<section class="cards four compact-stats">
    <div class="card stat"><h3>Shared inboxes</h3><p><?= count($sharedMailboxes) ?></p><span>Team queues ready for assignment</span></div>
    <div class="card stat alt"><h3>Saved views</h3><p><?= count($mailboxSavedViews ?? []) ?></p><span>Reusable queue filters for daily triage</span></div>
    <div class="card stat alt"><h3>Mail folders</h3><p><?= count($mailFolders ?? []) ?></p><span><?= count($mailLabels ?? []) ?> labels available</span></div>
    <div class="card stat alt"><h3>Queue health</h3><p><?= (int)($queueStats['queued_jobs'] ?? 0) ?></p><span><?= (int)($queueStats['failed_jobs'] ?? 0) ?> failed jobs across the system</span></div>
</section>

<section class="grid two">
    <div class="card">
        <div class="toolbar"><h2>Create shared inbox</h2><span class="badge">Team operations</span></div>
        <form method="post" action="index.php?route=messages/createSharedInbox" class="stacked-panel">
            <?= csrf_input() ?>
            <div class="form-grid two">
                <input name="name" placeholder="Shared inbox name" required>
                <input name="email_address" placeholder="support@example.com" required>
            </div>
            <textarea name="description" rows="4" placeholder="Describe who owns this inbox, what belongs here, and the SLA expectation."></textarea>
            <button class="btn primary">Save shared inbox</button>
        </form>
        <div class="small muted mt-12">Use one inbox per team queue such as Support, Sales, Renewals, or Campaign Replies.</div>
    </div>
    <div class="card">
        <div class="toolbar"><h2>Recommended workflow setup</h2><span class="badge">Best practice</span></div>
        <div class="alert-list">
            <article class="alert-tile ok"><strong>Ownership</strong><div class="small muted">Pair each shared inbox with a default owner or rotation rule to avoid double replies.</div></article>
            <article class="alert-tile ok"><strong>Views</strong><div class="small muted">Save views like Unassigned, Needs Approval, Waiting on Client, VIP, and Campaign Replies.</div></article>
            <article class="alert-tile warn"><strong>SLA states</strong><div class="small muted">Use normal / warning / breached states so Mail Center highlights overdue threads fast.</div></article>
            <article class="alert-tile warn"><strong>Labels</strong><div class="small muted">Keep folders simple, then layer labels like VIP, Invoice, Quote, Renewal, or Escalation.</div></article>
        </div>
    </div>
</section>

<section class="card">
    <div class="toolbar"><h2>Shared inbox inventory</h2><span class="badge"><?= count($sharedMailboxes) ?></span></div>
    <table>
        <thead><tr><th>Name</th><th>Email</th><th>Description</th><th>Queue suggestions</th></tr></thead>
        <tbody>
        <?php foreach ($sharedMailboxes as $mailbox): ?>
            <tr>
                <td><strong><?= e($mailbox['name'] ?? '') ?></strong></td>
                <td><?= e($mailbox['email_address'] ?? '') ?></td>
                <td><?= e($mailbox['description'] ?? '—') ?></td>
                <td class="small muted">Saved views, automations, and assignment rules can target this inbox.</td>
            </tr>
        <?php endforeach; ?>
        <?php if (!$sharedMailboxes): ?><tr><td colspan="4" class="muted">No shared inboxes created yet.</td></tr><?php endif; ?>
        </tbody>
    </table>
</section>

<section class="grid two">
    <div class="card">
        <div class="toolbar"><h2>Users available for ownership</h2><span class="badge"><?= count($users) ?></span></div>
        <div class="metric-strip">
            <?php foreach ($users as $user): ?>
                <div class="metric-chip"><strong><?= e($user['name'] ?? '') ?></strong><span class="small muted"><?= e($user['role'] ?? '') ?></span></div>
            <?php endforeach; ?>
            <?php if (!$users): ?><span class="muted">No users found.</span><?php endif; ?>
        </div>
    </div>
    <div class="card">
        <div class="toolbar"><h2>Folder and label structure</h2><span class="badge">Mail Center</span></div>
        <div class="metric-strip">
            <?php foreach (($mailFolders ?? []) as $folder): ?>
                <div class="metric-chip"><strong><?= e($folder['display_name'] ?? $folder['name'] ?? 'Folder') ?></strong><span class="small muted">folder</span></div>
            <?php endforeach; ?>
            <?php foreach (($mailLabels ?? []) as $label): ?>
                <div class="metric-chip"><strong><?= e($label['name'] ?? 'Label') ?></strong><span class="small muted">label</span></div>
            <?php endforeach; ?>
        </div>
    </div>
</section>
