<?php
// api/live_feed.php
require_once __DIR__ . '/../config.php';
startSecureSession();
if (empty($_SESSION['admin_id'])) { jsonOut(['error'=>'Unauthorized'], 401); }

$db      = db();
$adminId = (int)$_SESSION['admin_id'];
$rows = $db->prepare(
    'SELECT g.name, g.check_in_time, g.check_in_method, e.title AS event_title
     FROM guests g JOIN events e ON e.id=g.event_id
     WHERE g.checked_in=1 AND e.admin_id=?
     ORDER BY g.check_in_time DESC LIMIT 10'
);
$rows->execute([$adminId]);
$items = $rows->fetchAll();

ob_start();
foreach ($items as $ci):
?>
<div class="flex items-center gap-3" style="padding:10px 0;border-bottom:1px solid var(--border)">
  <div style="width:32px;height:32px;border-radius:50%;background:var(--surface-2);
              display:flex;align-items:center;justify-content:center;
              font-family:var(--font-display);color:var(--gold);flex-shrink:0">
    <?= mb_substr($ci['name'],0,1) ?>
  </div>
  <div style="flex:1;min-width:0">
    <div style="font-size:.88rem;font-weight:500;white-space:nowrap;overflow:hidden;text-overflow:ellipsis">
      <?= htmlspecialchars($ci['name']) ?>
    </div>
    <div class="text-muted text-sm"><?= htmlspecialchars($ci['event_title']) ?></div>
  </div>
  <div style="text-align:right;flex-shrink:0">
    <span class="badge <?= $ci['check_in_method']==='qr'?'badge-info':'badge-gold' ?>">
      <?= strtoupper($ci['check_in_method'] ?? '') ?>
    </span>
    <div class="text-muted" style="font-size:.68rem;margin-top:3px">
      <?= $ci['check_in_time'] ? date('H:i', strtotime($ci['check_in_time'])) : '' ?>
    </div>
  </div>
</div>
<?php endforeach;
$html = ob_get_clean();
jsonOut(['html' => $html]);
