<?php
$d = $data;
$hour = (int) date('G');
$hi = $hour < 12 ? 'Good morning' : ($hour < 17 ? 'Good afternoon' : 'Good evening');
$industry = $d['industry'] ?? ['letter' => 'A', 'name' => 'Distribution', 'summary' => ''];
$industryMeta = [
    'distribution' => ['bi-box-seam-fill', '#2563eb', '#eff6ff'],
    'software' => ['bi-laptop-fill', '#7c3aed', '#f3e8ff'],
    'contract' => ['bi-building-fill', '#ea580c', '#fff7ed'],
    'service' => ['bi-tools', '#0f766e', '#f0fdfa'],
];
$code = (string) ($industry['code'] ?? 'distribution');
$im = $industryMeta[$code] ?? $industryMeta['distribution'];
$kpis = [
    [(string) (int) ($d['users'] ?? 0), 'Can log in', 'bi-person-check-fill', '#16a34a', '#f0fdf4', url('users')],
    [(string) (int) ($d['stopped'] ?? 0), 'Stopped', 'bi-person-x-fill', '#dc2626', '#fef2f2', url('users')],
    [(string) count($d['roles'] ?? []), 'Roles', 'bi-shield-check', '#2563eb', '#eff6ff', url('roles')],
    [(string) (int) ($d['unsubscribes'] ?? 0), 'Unsubscribes', 'bi-slash-circle-fill', '#ea580c', '#fff7ed', url('unsubscribes')],
];
$roleMeta = [
    'salesperson' => ['bi-briefcase-fill', '#2563eb', '#eff6ff'],
    'manager' => ['bi-diagram-3-fill', '#7c3aed', '#f3e8ff'],
    'admin' => ['bi-shield-lock-fill', '#ea580c', '#fff7ed'],
    'marketing' => ['bi-megaphone-fill', '#db2777', '#fce7f3'],
    'service' => ['bi-tools', '#0f766e', '#f0fdfa'],
];
$people = $d['people'] ?? [];
$roles = $d['roles'] ?? [];
$roleCounts = $d['roleCounts'] ?? [];
$roleActive = $d['roleActive'] ?? [];
$activeN = (int) ($d['users'] ?? 0);
$stoppedN = (int) ($d['stopped'] ?? 0);
$peopleN = $activeN + $stoppedN;
$avatarTones = [
    ['#2563eb', '#dbeafe'],
    ['#7c3aed', '#ede9fe'],
    ['#0f766e', '#ccfbf1'],
    ['#db2777', '#fce7f3'],
    ['#ea580c', '#ffedd5'],
];
$jumps = [
    ['Users', (int) ($d['users'] ?? 0), 'bi-person-gear', url('users'), '#2563eb', '#eff6ff'],
    ['Roles', count($d['roles'] ?? []), 'bi-shield-check', url('roles'), '#7c3aed', '#f3e8ff'],
    ['Settings', $industry['letter'] ?? 'A', 'bi-buildings', url('settings'), '#0f766e', '#f0fdfa'],
    ['Setup', 'Go', 'bi-sliders', url('setup'), '#2563eb', '#eff6ff'],
    ['Customer Portal', 'Go', 'bi-door-open', url('settings'), '#0f766e', '#f0fdfa'],
    ['Unsubscribes', (int) ($d['unsubscribes'] ?? 0), 'bi-slash-circle-fill', url('unsubscribes'), '#dc2626', '#fef2f2'],
];
?>
<div class="adm-page">
  <div class="d-flex flex-wrap justify-content-between align-items-start gap-3 dash-head">
    <div>
      <h1 class="page-title"><?= e($hi) ?>, <?= e(current_user()['first_name']) ?></h1>
      <p class="page-sub mb-0">Add people, pick their roles, pick the industry setup, and turn smart tips on or off in Settings.</p>
    </div>
    <div class="d-flex align-items-center gap-2">
      <div class="dash-date"><i class="bi bi-calendar3"></i> <?= date('M j, Y') ?></div>
      <a class="btn btn-primary" href="<?= e(url('users')) ?>">Manage users</a>
    </div>
  </div>

  <?php if (dash_on('kpis')): ?>
  <div class="task-summary mb-3">
    <?php foreach ($kpis as $k): ?>
      <a class="task-summary-card" href="<?= e($k[5]) ?>" 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>
      </a>
    <?php endforeach; ?>
  </div>
  <?php endif; ?>

  <?php if (dash_on('jumps')): ?>
  <div class="mkt-jumps mb-3">
    <?php foreach ($jumps as $j): ?>
      <a class="mkt-jump" href="<?= e($j[3]) ?>" style="--task-accent: <?= e($j[4]) ?>; --task-tint: <?= e($j[5]) ?>">
        <span class="task-summary-icon"><i class="bi <?= e($j[2]) ?>"></i></span>
        <span class="min-w-0">
          <span class="mkt-jump-n"><?= e((string) $j[1]) ?></span>
          <span class="mkt-jump-l"><?= e($j[0]) ?></span>
        </span>
        <i class="bi bi-chevron-right mkt-jump-go"></i>
      </a>
    <?php endforeach; ?>
  </div>
  <?php endif; ?>

  <div class="row g-3">
    <div class="col-lg-7">
      <div class="card-soft ap-card h-100">
        <div class="ap-head">
          <div class="ap-head-icon"><i class="bi bi-people-fill"></i></div>
          <div class="min-w-0 flex-grow-1">
            <h2>People</h2>
            <p><?= $peopleN ? $peopleN . ' in this company' : 'Add the first person who should sign in' ?></p>
          </div>
          <a href="<?= e(url('users')) ?>">View all</a>
        </div>
        <?php if ($people): ?>
          <div class="ap-pills">
            <span class="ap-pill is-on"><i class="bi bi-person-check"></i> <?= $activeN ?> can log in</span>
            <?php if ($stoppedN): ?>
              <span class="ap-pill is-off"><i class="bi bi-person-x"></i> <?= $stoppedN ?> stopped</span>
            <?php endif; ?>
          </div>
          <div class="ap-list">
            <?php foreach ($people as $p):
                $name = trim(($p['first_name'] ?? '') . ' ' . ($p['last_name'] ?? ''));
                if ($name === '') {
                    $name = 'Untitled';
                }
                $on = ($p['login_status'] ?? '') === 'active';
                $tone = $avatarTones[((int) ($p['id'] ?? 0)) % count($avatarTones)];
                $roleBits = array_values(array_filter(array_map('trim', explode(',', (string) ($p['role_names'] ?? '')))));
            ?>
              <a class="ap-item <?= $on ? '' : 'is-off' ?>" href="<?= e(url('users')) ?>">
                <span class="ap-avatar" style="--ap-av: <?= e($tone[0]) ?>; --ap-av-bg: <?= e($tone[1]) ?>"><?= e(name_initials($name)) ?></span>
                <span class="min-w-0 flex-grow-1">
                  <span class="ap-name"><?= e($name) ?></span>
                  <span class="ap-meta">
                    <span class="ap-mail text-truncate"><?= e($p['email'] ?? '') ?></span>
                    <?php foreach ($roleBits as $rn): ?>
                      <span class="ap-role"><?= e($rn) ?></span>
                    <?php endforeach; ?>
                  </span>
                </span>
                <span class="ap-side">
                  <span class="ap-status <?= $on ? 'is-on' : 'is-off' ?>"><?= $on ? 'Active' : 'Stopped' ?></span>
                  <span class="ap-when"><?= e(!empty($p['last_login_at']) ? relative_when($p['last_login_at']) : 'Never signed in') ?></span>
                </span>
              </a>
            <?php endforeach; ?>
          </div>
        <?php else: ?>
          <div class="ap-empty">
            <i class="bi bi-person-plus"></i>
            <strong>No people yet</strong>
            <span>Add someone under Users and pick a role so they can sign in.</span>
            <a class="btn btn-primary mt-3" href="<?= e(url('users')) ?>">Add user</a>
          </div>
        <?php endif; ?>
      </div>
    </div>
    <div class="col-lg-5">
      <div class="card-soft ar-card mb-3">
        <div class="ar-head">
          <div class="ar-head-icon"><i class="bi bi-shield-lock-fill"></i></div>
          <div class="min-w-0 flex-grow-1">
            <h2>Roles</h2>
            <p>What each person can open. Assign them on Users.</p>
          </div>
          <a href="<?= e(url('roles')) ?>">View all</a>
        </div>
        <div class="ar-list">
          <?php foreach ($roles as $role):
              $code = (string) ($role['code'] ?? '');
              $m = $roleMeta[$code] ?? ['bi-person-fill', '#2563eb', '#eff6ff'];
              $n = (int) ($roleActive[$code] ?? 0);
              $allN = (int) ($roleCounts[$code] ?? 0);
          ?>
            <a class="ar-item" href="<?= e(url('users') . '?role=' . rawurlencode($code)) ?>" style="--ar-accent: <?= e($m[1]) ?>; --ar-tint: <?= e($m[2]) ?>">
              <span class="ar-icon"><i class="bi <?= e($m[0]) ?>"></i></span>
              <span class="min-w-0 flex-grow-1">
                <span class="ar-name"><?= e($role['name']) ?></span>
                <span class="ar-sum"><?= e($role['description'] ?? '') ?></span>
              </span>
              <span class="ar-count">
                <strong><?= $n ?></strong>
                <em><?= $n === 1 ? 'person' : 'people' ?></em>
                <?php if ($allN > $n): ?><small><?= (int) ($allN - $n) ?> stopped</small><?php endif; ?>
              </span>
            </a>
          <?php endforeach; ?>
        </div>
      </div>
      <a class="adm-setup" href="<?= e(url('settings')) ?>" style="--set-accent: <?= e($im[1]) ?>; --set-tint: <?= e($im[2]) ?>">
        <span class="set-industry-icon"><i class="bi <?= e($im[0]) ?>"></i></span>
        <span class="min-w-0">
          <span class="set-industry-kicker">Setup <?= e($industry['letter'] ?? 'A') ?></span>
          <span class="set-industry-name d-block"><?= e($industry['name'] ?? 'Distribution') ?></span>
          <span class="set-industry-sum d-block"><?= e($industry['summary'] ?? '') ?></span>
        </span>
        <i class="bi bi-chevron-right mkt-jump-go"></i>
      </a>
    </div>
  </div>
</div>
