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

<div class="mb-3">
    <h1 class="h3 mb-1">Guest Message Drafts</h1>
    <p class="text-muted mb-0">AI-drafted guest messages awaiting your review — nothing sends until you click Send, or the tenant has explicitly enabled auto-send for that category under Settings &gt; AI Features. Sending uses your hotel's real Hubtel SMS credits.</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 <?= $category === null ? 'active' : '' ?>" href="<?= site_url('guest-message-drafts') ?>">All <span class="badge bg-secondary ms-1"><?= $countAll ?></span></a></li>
            <?php foreach ($categories as $key => $label): ?>
                <li class="nav-item"><a class="nav-link <?= $category === $key ? 'active' : '' ?>" href="<?= site_url('guest-message-drafts?category=' . $key) ?>"><?= esc($label) ?></a></li>
            <?php endforeach; ?>
        </ul>
    </div>
    <div class="list-group list-group-flush">
        <?php if (empty($drafts)): ?>
            <div class="list-group-item text-center text-muted py-5">No drafts awaiting review.</div>
        <?php endif; ?>
        <?php foreach ($drafts as $draft): ?>
            <div class="list-group-item">
                <div class="d-flex justify-content-between align-items-start gap-3">
                    <div class="flex-grow-1">
                        <div class="d-flex align-items-center gap-2 mb-1">
                            <span class="badge bg-light text-dark border"><?= esc($categories[$draft->category] ?? $draft->category) ?></span>
                            <span class="fw-semibold"><?= esc($draft->guest_name ?? 'Unknown guest') ?></span>
                            <span class="text-muted small"><?= esc($draft->to_phone) ?></span>
                        </div>
                        <p class="small mb-1 border rounded p-2 bg-light"><?= esc($draft->draft_text) ?></p>
                        <span class="text-muted" style="font-size: 0.72rem;">Drafted <?= esc(date('d M Y, g:i a', strtotime($draft->created_at))) ?></span>
                    </div>
                    <div class="d-flex flex-column gap-1" style="min-width: 110px;">
                        <form action="<?= site_url('guest-message-drafts/' . $draft->id . '/send') ?>" method="post" onsubmit="return confirm('Send this real SMS to ' + <?= json_encode($draft->to_phone) ?> + '?');">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-primary w-100"><i class="bi bi-send me-1"></i>Send</button>
                        </form>
                        <form action="<?= site_url('guest-message-drafts/' . $draft->id . '/dismiss') ?>" method="post">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-outline-secondary w-100">Dismiss</button>
                        </form>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>

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