/* ── Helpers partagés Admin ───────────────────────────────────── */
export function fmt(iso) {
  if (!iso) return '—';
  const utc = iso.includes('T') || iso.endsWith('Z')
    ? iso
    : iso.replace(' ', 'T') + 'Z';
  return new Date(utc).toLocaleString('fr-FR', {
    day: '2-digit', month: '2-digit', year: 'numeric',
    hour: '2-digit', minute: '2-digit',
  });
}

export function Badge({ role }) {
  const isAdmin = role === 'admin';
  return (
    <span style={{
      display: 'inline-block', padding: '2px 10px', borderRadius: 12,
      fontSize: 11, fontWeight: 700, letterSpacing: '.04em',
      background: isAdmin ? 'rgba(234,179,8,.15)' : 'rgba(100,116,139,.15)',
      color: isAdmin ? '#ca8a04' : '#64748b',
      border: `1px solid ${isAdmin ? 'rgba(234,179,8,.35)' : 'rgba(100,116,139,.25)'}`,
    }}>
      {isAdmin ? 'Admin' : 'Utilisateur'}
    </span>
  );
}

/** Badge statut utilisateur — dérivé de status + email_verified */
export function UserStatusBadge({ status, emailVerified }) {
  // status: 'active' | 'deactivated' | 'locked'
  // Le statut affiché est une combinaison des deux champs
  let label, bg, color, border;
  if (status === 'deactivated') {
    label = 'Désactivé'; bg = 'rgba(100,116,139,.12)'; color = '#64748b'; border = 'rgba(100,116,139,.3)';
  } else if (status === 'locked') {
    label = 'Verrouillé'; bg = 'rgba(239,68,68,.12)'; color = '#dc2626'; border = 'rgba(239,68,68,.3)';
  } else if (!emailVerified) {
    label = 'En attente'; bg = 'rgba(245,158,11,.12)'; color = '#d97706'; border = 'rgba(245,158,11,.3)';
  } else {
    label = 'Actif'; bg = 'rgba(34,197,94,.12)'; color = '#16a34a'; border = 'rgba(34,197,94,.3)';
  }
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 5,
      padding: '2px 10px', borderRadius: 12,
      fontSize: 11, fontWeight: 700,
      background: bg, color, border: `1px solid ${border}`,
    }}>
      <span style={{ width: 6, height: 6, borderRadius: '50%', background: color, display: 'inline-block', flexShrink: 0 }} />
      {label}
    </span>
  );
}

export function StatusBadge({ status }) {
  const ok = status === 'ok';
  return (
    <span style={{
      display: 'inline-block', padding: '2px 10px', borderRadius: 12,
      fontSize: 11, fontWeight: 700,
      background: ok ? 'rgba(34,197,94,.12)' : 'rgba(239,68,68,.12)',
      color: ok ? '#16a34a' : '#dc2626',
      border: `1px solid ${ok ? 'rgba(34,197,94,.3)' : 'rgba(239,68,68,.3)'}`,
    }}>
      {ok ? 'OK' : 'Erreur'}
    </span>
  );
}
