<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="mb-3">
    <h1 class="h3 mb-1">WhatsApp Concierge</h1>
    <p class="text-muted mb-0">Every real inbound WhatsApp conversation the AI concierge has handled or flagged. See Settings &gt; AI Features to enable it or turn on auto-send.</p>
</div>

<div class="card shadow-sm">
    <div class="card-header p-0">
        <ul class="nav nav-tabs border-0 px-2 pt-2">
            <li class="nav-item"><a class="nav-link <?= $filter === null ? 'active' : '' ?>" href="<?= site_url('concierge') ?>">All</a></li>
            <li class="nav-item"><a class="nav-link <?= $filter === 'needs_staff' ? 'active' : '' ?>" href="<?= site_url('concierge?filter=needs_staff') ?>">Needs Staff <span class="badge bg-danger ms-1"><?= $needsStaffCount ?></span></a></li>
            <li class="nav-item"><a class="nav-link <?= $filter === 'pending_reply' ? 'active' : '' ?>" href="<?= site_url('concierge?filter=pending_reply') ?>">Awaiting Send <span class="badge bg-warning text-dark ms-1"><?= $pendingReplyCount ?></span></a></li>
        </ul>
    </div>
    <div class="list-group list-group-flush">
        <?php if (empty($conversations)): ?>
            <div class="list-group-item text-center text-muted py-5">No conversations here.</div>
        <?php endif; ?>
        <?php foreach ($conversations as $c): ?>
            <a href="<?= site_url('concierge/' . $c->id) ?>" class="list-group-item list-group-item-action">
                <div class="d-flex justify-content-between align-items-center gap-3">
                    <div>
                        <span class="fw-semibold"><?= esc($c->guest_name ?? 'Unrecognized number') ?></span>
                        <span class="text-muted small ms-2"><?= esc($c->phone) ?></span>
                    </div>
                    <div class="d-flex align-items-center gap-2">
                        <?php if ($c->needs_staff && ! $c->resolved_at): ?><span class="badge bg-danger">Needs Staff</span><?php endif; ?>
                        <?php if ($c->pending_reply): ?><span class="badge bg-warning text-dark">Awaiting Send</span><?php endif; ?>
                        <?php if ($c->resolved_at): ?><span class="badge bg-secondary">Resolved</span><?php endif; ?>
                        <span class="text-muted" style="font-size: 0.72rem;"><?= $c->last_message_at ? esc(date('d M Y, g:i a', strtotime($c->last_message_at))) : '' ?></span>
                    </div>
                </div>
            </a>
        <?php endforeach; ?>
    </div>
</div>

<?= $this->endSection() ?>
