export function normalizeRole(rawRole: unknown): string {
  if (typeof rawRole !== "string") return "";
  const role = rawRole.trim().toLowerCase();
  if (role === "tenant_member") return "user";
  if (role === "admin") return "tenant_admin";
  if (role === "superadmin") return "super_admin";
  if (role === "tenantadmin") return "tenant_admin";
  if (role === "rpadmin") return "rp_admin";
  return role;
}

export function resolveProfileRole(
  profile: Record<string, unknown> | undefined,
) {
  if (!profile) return "";
  const candidates = [
    profile.role,
    profile.grade,
    profile["custom:role"],
    profile["custom:grade"],
  ];
  for (const candidate of candidates) {
    const normalized = normalizeRole(candidate);
    if (normalized) return normalized;
  }
  return "";
}
