<?php
$sharedMailboxes = $sharedMailboxes ?? [];
$queueStats = $queueStats ?? [];
$users = $users ?? [];
$mailQueueSummary = $mailQueueSummary ?? ['assigned_to_me' => 0, 'unassigned' => 0, 'pending_approval' => 0, 'breached' => 0, 'unread' => 0];
?>
<section class="workspace-hero">
    <div>
        <h2>Shared Inboxes</h2>
        <p>Run team email queues from one place with ownership, approvals, SLA triage, and direct deep links back into 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/syncHealth">Sync Health</a>
    </div>
</section>

<section class="cards four compact-stats">
    <div class="card stat"><h3>Shared inboxes</h3><p><?= count($sharedMailboxes) ?></p><span>Team queues with routing and ownership.</span></div>
    <div class="card stat alt"><h3>My queue</h3><p><?= (int)($mailQueueSummary['assigned_to_me'] ?? 0) ?></p><span>Assigned or claimed by you.</span></div>
    <div class="card stat alt"><h3>Unassigned</h3><p><?= (int)($mailQueueSummary['unassigned'] ?? 0) ?></p><span>Threads still waiting for an owner.</span></div>
    <div class="card stat alt"><h3>Approvals / SLA</h3><p><?= (int)($mailQueueSummary['pending_approval'] ?? 0) ?></p><span><?= (int)($mailQueueSummary['breached'] ?? 0) ?> breached SLA threads.</span></div>
</section>

<section class="card">
    <div class="toolbar"><h2>Work queue shortcuts</h2><span class="badge">Mail Center</span></div>
    <div class="mail-chip-list">
        <a class="mail-chip color-slate" href="index.php?route=messages/mailCenter&ownership=mine">Assigned / claimed by me</a>
        <a class="mail-chip color-slate" href="index.php?route=messages/mailCenter&ownership=unassigned">Unassigned threads</a>
        <a class="mail-chip color-amber" href="index.php?route=messages/mailCenter&approval=pending">Pending approval</a>
        <a class="mail-chip color-red" href="index.php?route=messages/mailCenter&sla=breached">Breached SLA</a>
        <a class="mail-chip color-blue" href="index.php?route=messages/mailCenter&q=is:unread">Unread mail</a>
        <a class="mail-chip color-slate" href="index.php?route=messages/mailCenter&q=has:attachment">Has attachment</a>
    </div>
    <div class="small muted" style="margin-top:8px;">Use these links as your default service queue views, or save filtered mailbox views inside Mail Center for daily operations.</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">Create queues such as Support, Renewals, Sales, VIP Service, 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">Claim or assign every live customer conversation so two teammates do not reply at once.</div></article>
            <article class="alert-tile ok"><strong>Approval lane</strong><div class="small muted">Use pending approval for regulated or high-risk replies before they leave the queue.</div></article>
            <article class="alert-tile warn"><strong>SLA focus</strong><div class="small muted">Sort by breached and due soon to surface the service backlog before it becomes customer-visible.</div></article>
            <article class="alert-tile warn"><strong>Saved views</strong><div class="small muted">Inside Mail Center, save standard views like Unassigned, My Queue, Needs Approval, and Breached SLA.</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>Queue health</th><th>Unread</th><th>Open in Mail Center</th></tr></thead>
        <tbody>
        <?php foreach ($sharedMailboxes as $mailbox): ?>
            <?php $stats = $mailbox['stats'] ?? []; ?>
            <tr>
                <td>
                    <strong><?= e($mailbox['name'] ?? '') ?></strong><br>
                    <span class="small muted"><?= e($mailbox['description'] ?? 'Shared service queue') ?></span>
                </td>
                <td><?= e($mailbox['email_address'] ?? '') ?></td>
                <td>
                    <div class="small">Threads <?= (int)($stats['thread_count'] ?? 0) ?> · Unassigned <?= (int)($stats['unassigned_count'] ?? 0) ?></div>
                    <div class="small muted">Mine <?= (int)($stats['assigned_to_me_count'] ?? 0) ?> · Pending approval <?= (int)($stats['pending_approval_count'] ?? 0) ?> · Breached <?= (int)($stats['breached_count'] ?? 0) ?></div>
                </td>
                <td><?= (int)($stats['unread_count'] ?? 0) ?></td>
                <td>
                    <div style="display:flex;gap:8px;flex-wrap:wrap;">
                        <a class="btn secondary small" href="index.php?route=messages/mailCenter&shared_mailbox_id=<?= (int)$mailbox['id'] ?>">All</a>
                        <a class="btn secondary small" href="index.php?route=messages/mailCenter&shared_mailbox_id=<?= (int)$mailbox['id'] ?>&ownership=unassigned">Unassigned</a>
                        <a class="btn secondary small" href="index.php?route=messages/mailCenter&shared_mailbox_id=<?= (int)$mailbox['id'] ?>&approval=pending">Needs approval</a>
                    </div>
                </td>
            </tr>
        <?php endforeach; ?>
        <?php if (!$sharedMailboxes): ?><tr><td colspan="5" 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>System queue health</h2><span class="badge">Workers</span></div>
        <div class="metric-strip">
            <div class="metric-chip"><strong><?= (int)($queueStats['queued_jobs'] ?? 0) ?></strong><span class="small muted">queued jobs</span></div>
            <div class="metric-chip"><strong><?= (int)($queueStats['processing_jobs'] ?? 0) ?></strong><span class="small muted">processing</span></div>
            <div class="metric-chip"><strong><?= (int)($queueStats['failed_jobs'] ?? 0) ?></strong><span class="small muted">failed</span></div>
            <div class="metric-chip"><strong><?= (int)($mailQueueSummary['unread'] ?? 0) ?></strong><span class="small muted">unread threads</span></div>
        </div>
    </div>
</section>
