<?php
$meetings = $meetings ?? [];
$counts = ['call' => 0, 'meeting' => 0, 'visit' => 0];
foreach ($meetings as $row) {
    $type = (string) ($row['meeting_type'] ?? 'meeting');
    if ($type !== 'call' && $type !== 'visit') {
        $type = 'meeting';
    }
    $counts[$type]++;
}
$today = date('Y-m-d');
$tomorrow = date('Y-m-d', strtotime('+1 day'));
?>
<div class="card-soft up-card h-100">
  <div class="up-head">
    <div class="up-head-icon"><i class="bi bi-calendar2-week"></i></div>
    <div class="min-w-0 flex-grow-1">
      <h2>Upcoming calls/meetings/visits</h2>
      <p><?= $meetings ? count($meetings) . ' coming up' : 'What’s next on your calendar' ?></p>
    </div>
    <a href="<?= e(url('calendar')) ?>">Calendar</a>
  </div>
  <?php if ($meetings): ?>
  <div class="up-pills">
    <?php if ($counts['call']): ?>
      <span class="up-pill is-call"><i class="bi bi-telephone"></i> <?= (int) $counts['call'] ?> call<?= $counts['call'] === 1 ? '' : 's' ?></span>
    <?php endif; ?>
    <?php if ($counts['meeting']): ?>
      <span class="up-pill is-meeting"><i class="bi bi-people"></i> <?= (int) $counts['meeting'] ?> meeting<?= $counts['meeting'] === 1 ? '' : 's' ?></span>
    <?php endif; ?>
    <?php if ($counts['visit']): ?>
      <span class="up-pill is-visit"><i class="bi bi-geo-alt"></i> <?= (int) $counts['visit'] ?> visit<?= $counts['visit'] === 1 ? '' : 's' ?></span>
    <?php endif; ?>
  </div>
  <div class="up-list">
    <?php foreach ($meetings as $m):
        $kind = meeting_kind($m);
        $type = (string) ($m['meeting_type'] ?? 'meeting');
        $low = strtolower($kind . ' ' . ($m['location'] ?? ''));
        if ($type === 'call' || str_contains($low, 'phone')) {
            $cls = 'call';
            $icon = 'bi-telephone';
        } elseif ($type === 'visit') {
            $cls = 'visit';
            $icon = 'bi-geo-alt';
        } else {
            $cls = 'meeting';
            $icon = (str_contains($low, 'zoom') || str_contains($low, 'teams')) ? 'bi-camera-video' : 'bi-people';
        }
        $ts = strtotime((string) $m['starts_at']);
        $day = $ts ? date('Y-m-d', $ts) : '';
        $when = $day === $today ? 'Today' : ($day === $tomorrow ? 'Tomorrow' : ($ts ? date('D', $ts) : ''));
        $who = trim((string) ($m['related_name'] ?? ''));
    ?>
      <a class="up-item is-<?= e($cls) ?><?= $day === $today ? ' is-today' : '' ?>" href="<?= e(url('calendar')) ?>">
        <div class="up-date">
          <span class="mon"><?= $ts ? e(strtoupper(date('M', $ts))) : '—' ?></span>
          <span class="day"><?= $ts ? e(date('j', $ts)) : '' ?></span>
        </div>
        <div class="up-body min-w-0">
          <div class="up-title text-truncate"><?= e($m['title'] ?? 'Untitled') ?></div>
          <div class="up-meta">
            <span class="up-when"><?= e($when) ?><?php if ($ts): ?> · <?= e(dt_fmt($m['starts_at'], 'g:i A')) ?><?php endif; ?><?php if (!empty($m['ends_at'])): ?>–<?= e(dt_fmt($m['ends_at'], 'g:i A')) ?><?php endif; ?></span>
            <span class="up-kind"><i class="bi <?= e($icon) ?>"></i> <?= e($kind) ?></span>
          </div>
          <?php if ($who !== ''): ?>
            <div class="up-who text-truncate"><?= e($who) ?></div>
          <?php endif; ?>
        </div>
      </a>
    <?php endforeach; ?>
  </div>
  <?php else: ?>
  <div class="up-empty">
    <i class="bi bi-calendar-check"></i>
    <strong>Nothing coming up</strong>
    <span>No calls, meetings, or visits on the calendar yet.</span>
  </div>
  <?php endif; ?>
</div>
