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

<div class="mb-3">
    <h1 class="h3 mb-1">AI Alerts</h1>
    <p class="text-muted mb-0">Notable week-over-week deviations in occupancy, revenue, no-shows, cancellations, or room-type bookings — flagged for review, never acted on automatically. Generated by <code>php spark ai:flag-anomalies</code>.</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 <?= $tab === 'open' ? 'active' : '' ?>" href="<?= site_url('ai/alerts?tab=open') ?>">Open <?php if ($openCount > 0): ?><span class="badge bg-danger ms-1"><?= $openCount ?></span><?php endif; ?></a></li>
            <li class="nav-item"><a class="nav-link <?= $tab === 'dismissed' ? 'active' : '' ?>" href="<?= site_url('ai/alerts?tab=dismissed') ?>">Dismissed</a></li>
        </ul>
    </div>
    <div class="list-group list-group-flush">
        <?php if (empty($alerts)): ?>
            <div class="list-group-item text-center text-muted py-5">
                <?= $tab === 'dismissed' ? 'No dismissed alerts.' : 'No open alerts — nothing notable flagged right now.' ?>
            </div>
        <?php endif; ?>
        <?php foreach ($alerts as $alert):
            $severityMeta = [
                'high'   => ['badge' => 'danger', 'label' => 'High'],
                'medium' => ['badge' => 'warning', 'label' => 'Medium'],
                'low'    => ['badge' => 'secondary', 'label' => 'Low'],
            ][$alert->severity] ?? ['badge' => 'secondary', 'label' => ucfirst($alert->severity)];
        ?>
            <div class="list-group-item">
                <div class="d-flex justify-content-between align-items-start gap-3">
                    <div>
                        <div class="d-flex align-items-center gap-2 mb-1">
                            <span class="badge bg-<?= $severityMeta['badge'] ?>"><?= esc($severityMeta['label']) ?></span>
                            <span class="fw-semibold"><?= esc($alert->title) ?></span>
                        </div>
                        <p class="small text-muted mb-1"><?= esc($alert->description) ?></p>
                        <span class="text-muted" style="font-size: 0.72rem;">Flagged <?= esc(date('d M Y', strtotime($alert->created_at))) ?><?php if ($alert->dismissed_at): ?> · Dismissed <?= esc(date('d M Y', strtotime($alert->dismissed_at))) ?><?php endif; ?></span>
                    </div>
                    <?php if ($tab === 'open'): ?>
                    <form action="<?= site_url('ai/alerts/' . $alert->id . '/dismiss') ?>" method="post" onsubmit="return confirm('Dismiss this alert?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-sm btn-outline-secondary"><i class="bi bi-check2 me-1"></i>Dismiss</button>
                    </form>
                    <?php endif; ?>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>

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