<?php
$roles = $roles ?? [];
$roleCounts = $roleCounts ?? [];
$roleActive = $roleActive ?? [];
$meta = [
    'salesperson' => ['bi-briefcase-fill', '#2563eb', '#eff6ff', [noun('leads', 'Leads'), account_label(), opportunity_label(), 'Email', 'Campaign reports']],
    'manager' => ['bi-diagram-3-fill', '#7c3aed', '#f3e8ff', ['Team pipeline', 'Reports', 'Lead assignment', 'Approvals']],
    'admin' => ['bi-shield-lock-fill', '#ea580c', '#fff7ed', ['Users', 'Roles', 'Settings', 'Customer Portal']],
    'marketing' => ['bi-megaphone-fill', '#db2777', '#fce7f3', ['Campaigns', 'Lists', 'Templates', 'Order lists']],
    'service' => ['bi-tools', '#0f766e', '#f0fdfa', [account_label(), 'Service visits', 'Service jobs', 'Equipment']],
];
$kpis = [
    [(string) count($roles), 'Roles', 'bi-shield-check', '#2563eb', '#eff6ff', null],
    [(string) (int) ($assigned ?? 0), 'People with a role', 'bi-people-fill', '#16a34a', '#f0fdf4', url('users')],
    [(string) (int) ($twoPlus ?? 0), 'Two roles', 'bi-person-plus-fill', '#7c3aed', '#f3e8ff', url('users')],
    [(string) (int) ($people ?? 0), 'In this company', 'bi-building', '#ea580c', '#fff7ed', url('users')],
];
?>
<div class="role-page">
  <div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-3">
    <div>
      <h1 class="page-title">Roles</h1>
      <p class="page-sub mb-0">A role decides which sections a person sees. You assign roles on the Users page. Change names and who sees what in Setup.</p>
    </div>
    <a class="btn btn-primary" href="<?= e(url('users')) ?>">Assign on Users</a>
  </div>

  <div class="task-summary mb-3">
    <?php foreach ($kpis as $k): ?>
      <<?= $k[5] ? 'a' : 'div' ?> class="task-summary-card" <?php if ($k[5]): ?>href="<?= e($k[5]) ?>"<?php endif; ?> style="--task-accent: <?= e($k[3]) ?>; --task-tint: <?= e($k[4]) ?>">
        <div class="task-summary-icon"><i class="bi <?= e($k[2]) ?>"></i></div>
        <div>
          <div class="task-summary-n"><?= e($k[0]) ?></div>
          <div class="task-summary-l"><?= e($k[1]) ?></div>
        </div>
      </<?= $k[5] ? 'a' : 'div' ?>>
    <?php endforeach; ?>
  </div>

  <div class="camp-compose-note mb-3">
    These five roles are fixed. Pick them on Users. Hide or open sections for each role in Setup → Permissions.
  </div>

  <div class="role-grid">
    <?php foreach ($roles as $r):
        $code = (string) ($r['code'] ?? '');
        $m = $meta[$code] ?? ['bi-person-fill', '#2563eb', '#eff6ff', []];
        $n = (int) ($roleActive[$code] ?? 0);
        $allN = (int) ($roleCounts[$code] ?? 0);
        $stopped = max(0, $allN - $n);
    ?>
      <article class="role-card" style="--task-accent: <?= e($m[1]) ?>; --task-tint: <?= e($m[2]) ?>">
        <div class="role-card-top">
          <div class="task-summary-icon"><i class="bi <?= e($m[0]) ?>"></i></div>
          <div class="min-w-0 flex-grow-1">
            <div class="lead-name"><?= e($r['name']) ?></div>
            <p class="role-sum mb-0"><?= e($r['description']) ?></p>
          </div>
          <a class="role-count" href="<?= e(url('users') . '?role=' . rawurlencode($code)) ?>">
            <span class="rpt-metric-n"><?= $n ?></span>
            <span class="rpt-metric-l"><?= $n === 1 ? 'person' : 'people' ?></span>
          </a>
        </div>
        <?php if ($m[3]): ?>
          <div class="lst-chips">
            <?php foreach ($m[3] as $chip): ?>
              <span class="lst-chip"><?= e($chip) ?></span>
            <?php endforeach; ?>
          </div>
        <?php endif; ?>
        <div class="role-card-foot">
          <?php if ($stopped > 0): ?>
            <span class="text-muted"><?= (int) $stopped ?> stopped</span>
          <?php else: ?>
            <span class="text-muted">No stopped logins</span>
          <?php endif; ?>
          <a href="<?= e(url('users') . '?role=' . rawurlencode($code)) ?>">View people</a>
        </div>
      </article>
    <?php endforeach; ?>
  </div>
</div>
