<?php
$recent = $recent ?? [];
$title = $title ?? 'Recent Communications';
$emptyTitle = $emptyTitle ?? 'No communications yet';
$emptyHint = $emptyHint ?? 'Calls, emails, and notes will show up here.';
$moreHref = $moreHref ?? (can_see('emails') ? url('emails') : '');
$moreLabel = $moreLabel ?? 'Emails';
$pillMap = $pillMap ?? ['email' => 'bi-envelope', 'call' => 'bi-telephone', 'note' => 'bi-sticky', 'meeting' => 'bi-calendar-event'];
$typeCounts = [];
foreach ($recent as $row) {
    $t = (string) ($row['type'] ?? 'note');
    $typeCounts[$t] = ($typeCounts[$t] ?? 0) + 1;
}
$n = count($recent);
$sub = $n ? $n . ' latest ' . ($n === 1 ? 'item' : 'items') : 'Nothing logged yet';
$typeLabel = static function (string $type): string {
    return match ($type) {
        'email' => 'Email',
        'call' => 'Call',
        'meeting' => 'Meeting',
        'note' => 'Note',
        'visit' => 'Visit',
        'campaign' => 'Campaign',
        'task' => 'Task',
        'stage' => 'Stage',
        default => ucfirst($type),
    };
};
?>
<div class="card-soft hc-card h-100">
  <div class="hc-head">
    <div class="hc-head-icon"><i class="bi bi-chat-dots"></i></div>
    <div class="min-w-0 flex-grow-1">
      <h2><?= e($title) ?></h2>
      <p><?= e($sub) ?></p>
    </div>
    <?php if ($moreHref !== ''): ?>
      <a href="<?= e($moreHref) ?>"><?= e($moreLabel) ?></a>
    <?php endif; ?>
  </div>
  <?php if ($n): ?>
  <div class="hc-pills">
    <?php foreach ($pillMap as $key => $icon):
        if (empty($typeCounts[$key])) {
            continue;
        }
        $label = $typeLabel($key);
        $plural = $typeCounts[$key] === 1 ? $label : (str_ends_with($label, 's') ? $label : $label . 's');
    ?>
      <span class="hc-pill is-<?= e($key) ?>"><i class="bi <?= e($icon) ?>"></i> <?= (int) $typeCounts[$key] ?> <?= e($plural) ?></span>
    <?php endforeach; ?>
  </div>
  <div class="hc-list">
    <?php foreach ($recent as $a):
        $st = activity_style($a['type'] ?? '');
        $kind = (string) ($a['type'] ?? 'note');
        $who = trim((string) ($a['related_name'] ?: $a['owner_name'] ?? ''));
        $href = related_url($a['related_type'] ?? null, (int) ($a['related_id'] ?? 0));
        if ($href === '#') {
            $href = $moreHref !== '' ? $moreHref : url('');
        }
        if ($kind === 'visit' && !empty($a['related_id'])) {
            $href = url('service/' . (int) $a['related_id']);
        }
    ?>
      <a class="hc-item is-<?= e($kind) ?>" href="<?= e($href) ?>">
        <span class="hc-mark" style="background:<?= e($st[1]) ?>22;color:<?= e($st[1]) ?>"><i class="bi <?= e($st[0]) ?>"></i></span>
        <span class="hc-body min-w-0">
          <span class="hc-title text-truncate"><?= e($a['subject'] ?: $typeLabel($kind)) ?></span>
          <span class="hc-meta">
            <span class="hc-kind"><?= e($typeLabel($kind)) ?></span>
            <?php if ($who !== ''): ?><span class="hc-who text-truncate"><?= e($who) ?></span><?php endif; ?>
          </span>
          <?= activity_file_chip($a['file_name'] ?? null, (int) $a['id']) ?>
        </span>
        <span class="hc-when"><?= e(relative_when($a['activity_at'] ?? null)) ?></span>
      </a>
    <?php endforeach; ?>
  </div>
  <?php else: ?>
  <div class="hc-empty">
    <i class="bi bi-chat-dots"></i>
    <strong><?= e($emptyTitle) ?></strong>
    <span><?= e($emptyHint) ?></span>
  </div>
  <?php endif; ?>
</div>
