<?php
$relatedType = (string) ($relatedType ?? '');
$relatedId = (int) ($relatedId ?? 0);
$feed = $feed ?? [];
$formClass = $formClass ?? '';
$emptyTitle = $emptyTitle ?? 'No activity yet';
$emptyHint = $emptyHint ?? 'Calls, emails, and notes will show up here.';
?>
<div class="card-soft rec-card p-3">
  <?php \App\Core\View::partial('record_section_head', [
      'icon' => 'bi-chat-dots',
      'title' => 'Activity',
      'hint' => 'What happened on this record. Log a note, call, or meeting below.',
  ]); ?>
  <?php \App\Core\View::partial('record_activity_form', [
      'relatedType' => $relatedType,
      'relatedId' => $relatedId,
      'formClass' => $formClass,
  ]); ?>
  <?php if (!$feed): ?>
    <div class="task-empty rec-empty">
      <div class="task-empty-icon"><i class="bi bi-chat-dots"></i></div>
      <h2><?= e($emptyTitle) ?></h2>
      <p><?= e($emptyHint) ?></p>
    </div>
  <?php else: ?>
    <div class="rec-activity">
      <?php foreach ($feed as $t):
          $style = activity_style($t['type'] ?? '');
          $kind = (string) ($t['type'] ?? 'note');
      ?>
        <div class="rec-activity-row">
          <span class="comm-icon" style="background:<?= e($style[1]) ?>22;color:<?= e($style[1]) ?>"><i class="bi <?= e($style[0]) ?>"></i></span>
          <div class="min-w-0 flex-grow-1">
            <div class="d-flex flex-wrap align-items-center gap-2">
              <span class="fw-semibold"><?= e($t['subject'] ?: ucfirst($kind)) ?></span>
              <span class="rec-chip"><?= e(ucfirst($kind)) ?></span>
            </div>
            <?php if (!empty($t['body'])): ?><div class="small mt-1"><?= nl2br(e($t['body'])) ?></div><?php endif; ?>
            <?= activity_file_chip($t['file_name'] ?? null, (int) $t['id']) ?>
            <div class="small text-muted mt-1"><?= e($t['owner_name'] ?: 'Someone') ?> · <?= e(dt_fmt($t['activity_at'], 'M j, Y g:i A')) ?></div>
          </div>
          <span class="small text-muted text-nowrap"><?= e(relative_when($t['activity_at'] ?? null)) ?></span>
        </div>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
</div>
