<?php
// task-comms/partials/layout/collaborators.php

// Support either $task (old) or $task_data (new cache)
$__src = [];
if (isset($task) && is_array($task)) {
  $__src = $task;
} elseif (isset($task_data) && is_array($task_data)) {
  $__src = $task_data;
}

$followers = $__src['followers'] ?? [];

$palette = [
  'bg-red-600','bg-blue-600','bg-green-600','bg-yellow-600',
  'bg-purple-600','bg-pink-600','bg-indigo-600','bg-teal-600',
  'bg-orange-600','bg-amber-600'
];
function afb_color_for_name($name, $palette) {
  $i = crc32((string)$name) % count($palette);
  return $palette[$i];
}
function afb_initials($name) {
  $parts = preg_split('/\s+/u', trim((string)$name));
  $a = mb_substr($parts[0] ?? 'U', 0, 1);
  $b = isset($parts[1]) ? mb_substr(end($parts), 0, 1) : '';
  $ini = mb_strtoupper($a . $b);
  return $ini !== '' ? $ini : 'U';
}
?>

<div class="gateway-card">
  <p class="gateway-label" style="margin-bottom:.875rem">Collaborators</p>

  <?php if (!empty($followers)): ?>
    <div class="flex flex-wrap gap-2">
      <?php foreach ($followers as $f): 
        $name = $f['name'] ?? '—';
        $email = sanitize_email($f['email'] ?? '');
        $initials = afb_initials($name);
        $bg = afb_color_for_name($name, $palette);
      ?>
        <span class="gateway-collab-chip">
          <span class="gateway-collab-avatar <?= $bg ?>"><?= esc_html($initials) ?></span>
          <span style="display:flex;flex-direction:column;min-width:0">
            <span style="font-size:.8125rem;color:#475569;max-width:13rem;overflow:hidden;text-overflow:ellipsis;white-space:nowrap" title="<?= esc_attr($name) ?>\"><?= esc_html($name) ?></span>
            <?php if (!empty($email)): ?>
              <span style="font-size:.6875rem;color:#94a3b8;max-width:13rem;overflow:hidden;text-overflow:ellipsis;white-space:nowrap" title="<?= esc_attr($email) ?>\"><?= esc_html($email) ?></span>
            <?php endif; ?>
          </span>
        </span>
      <?php endforeach; ?>
    </div>
  <?php else: ?>
    <p style="font-size:.875rem;color:#94a3b8">No collaborators assigned yet.</p>
  <?php endif; ?>
</div>
