export function normalizeRole(rawRole: unknown): string {
  if (typeof rawRole !== "string") return "";
  const role = rawRole.trim().toLowerCase();

  switch (role) {
    case "super_admin":
    case "superadmin":
    case "super-admin":
      return "super_admin";
    case "rp_admin":
    case "rpadmin":
    case "rp-admin":
      return "rp_admin";
    case "tenant_admin":
    case "tenantadmin":
    case "tenant-admin":
      return "tenant_admin";
    default:
      return "user";
  }
}

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 "";
}
