<?php
$tasks = $tasks ?? [];
$mode = (string) ($mode ?? 'all');
if (!in_array($mode, ['all', 'today', 'overdue'], true)) {
    $mode = 'all';
}
$todayKey = date('Y-m-d');
$todayTasks = [];
$overdueTasks = [];
foreach ($tasks as $t) {
    $dueAt = (string) ($t['due_at'] ?? '');
    $day = $dueAt !== '' ? date('Y-m-d', (int) strtotime($dueAt)) : '';
    if ($day === $todayKey) {
        $todayTasks[] = $t;
    } elseif ($day !== '' && $day < $todayKey) {
        $overdueTasks[] = $t;
    }
}
$todayN = count($todayTasks);
$overdueN = count($overdueTasks);
$groups = [];
$cardClass = 'ht-card';
if ($mode === 'today') {
    $groups = [['key' => 'today', 'label' => 'Today', 'icon' => 'bi-sun', 'rows' => $todayTasks]];
    $title = 'Today’s tasks';
    $sub = $todayN ? $todayN . ' due today' : 'Nothing due today';
    $emptyTitle = 'Nothing due today';
    $emptyHint = 'Tasks due today will show here.';
    $emptyIcon = 'bi-sun';
    $showPills = false;
} elseif ($mode === 'overdue') {
    $groups = [['key' => 'overdue', 'label' => 'Overdue', 'icon' => 'bi-exclamation-circle', 'rows' => $overdueTasks]];
    $title = 'Overdue tasks';
    $sub = $overdueN ? $overdueN . ' late' : 'Nothing overdue';
    $emptyTitle = 'Nothing overdue';
    $emptyHint = 'Late tasks will show here.';
    $emptyIcon = 'bi-exclamation-circle';
    $cardClass .= ' is-late';
    $showPills = false;
} else {
    $groups = [
        ['key' => 'today', 'label' => 'Today', 'icon' => 'bi-sun', 'rows' => $todayTasks],
        ['key' => 'overdue', 'label' => 'Overdue', 'icon' => 'bi-exclamation-circle', 'rows' => $overdueTasks],
    ];
    $title = 'Today’s tasks and overdue items';
    $sub = ($todayN || $overdueN)
        ? trim(($todayN ? $todayN . ' today' : '') . ($todayN && $overdueN ? ' · ' : '') . ($overdueN ? $overdueN . ' overdue' : ''))
        : 'What you need to finish';
    $emptyTitle = 'You’re caught up';
    $emptyHint = 'No tasks due today or late.';
    $emptyIcon = 'bi-check2-circle';
    $showPills = true;
}
$hasRows = false;
foreach ($groups as $group) {
    if ($group['rows']) {
        $hasRows = true;
        break;
    }
}
?>
<div class="card-soft <?= e($cardClass) ?> h-100">
  <div class="ht-head">
    <div class="ht-head-icon"><i class="bi <?= $mode === 'overdue' ? 'bi-exclamation-circle' : 'bi-check2-square' ?>"></i></div>
    <div class="min-w-0 flex-grow-1">
      <h2><?= e($title) ?></h2>
      <p><?= e($sub) ?></p>
    </div>
    <a href="<?= e(url('tasks')) ?>">Tasks</a>
  </div>
  <?php if ($hasRows): ?>
  <?php if ($showPills): ?>
  <div class="ht-pills">
    <?php if ($todayN): ?>
      <span class="ht-pill is-today"><i class="bi bi-sun"></i> <?= (int) $todayN ?> today</span>
    <?php endif; ?>
    <?php if ($overdueN): ?>
      <span class="ht-pill is-overdue"><i class="bi bi-exclamation-circle"></i> <?= (int) $overdueN ?> overdue</span>
    <?php endif; ?>
  </div>
  <?php endif; ?>
  <div class="ht-list">
    <?php foreach ($groups as $group):
        if (!$group['rows']) {
            continue;
        }
    ?>
      <?php if ($mode === 'all'): ?>
      <div class="ht-group-label is-<?= e($group['key']) ?>"><i class="bi <?= e($group['icon']) ?>"></i> <?= e($group['label']) ?></div>
      <?php endif; ?>
      <?php foreach ($group['rows'] as $t):
          $due = due_label($t['due_at'], $t['status'] ?? 'open');
          $prio = (string) ($t['priority'] ?? 'normal');
          $who = trim((string) ($t['related_name'] ?? ''));
          $href = url('tasks');
          $lateText = '';
          if ($group['key'] === 'overdue' && !empty($t['due_at'])) {
              $lateDays = max(1, (int) floor((strtotime($todayKey) - strtotime((string) $t['due_at'])) / 86400));
              $lateText = $lateDays === 1 ? '1 day late' : $lateDays . ' days late';
          }
      ?>
        <a class="ht-item is-<?= e($group['key']) ?><?= $prio === 'high' ? ' is-high' : '' ?>" href="<?= e($href) ?>">
          <span class="ht-mark"><i class="bi <?= $group['key'] === 'overdue' ? 'bi-exclamation-lg' : 'bi-check-lg' ?>"></i></span>
          <span class="ht-body min-w-0">
            <span class="ht-title text-truncate"><?= e($t['title'] ?: 'Untitled') ?></span>
            <span class="ht-meta">
              <?php if ($group['key'] === 'today' && !empty($t['due_at'])): ?>
                <span class="ht-when"><i class="bi bi-clock"></i> <?= e(dt_fmt($t['due_at'], 'g:i A')) ?></span>
              <?php elseif ($lateText !== ''): ?>
                <span class="ht-when"><?= e($lateText) ?></span>
              <?php endif; ?>
              <?php if ($prio === 'high'): ?><span class="ht-prio">High</span><?php endif; ?>
              <?php if ($who !== ''): ?><span class="ht-who text-truncate"><?= e($who) ?></span><?php endif; ?>
            </span>
          </span>
          <span class="ht-due <?= e($due['class']) ?>"><?= e($due['text']) ?></span>
        </a>
      <?php endforeach; ?>
    <?php endforeach; ?>
  </div>
  <?php else: ?>
  <div class="ht-empty">
    <i class="bi <?= e($emptyIcon) ?>"></i>
    <strong><?= e($emptyTitle) ?></strong>
    <span><?= e($emptyHint) ?></span>
  </div>
  <?php endif; ?>
</div>
