import { User, User_Role } from "@/types/proto/api/v1/user_service_pb";

export const isSuperUser = (user: User | undefined) => {
  return user && (user.role === User_Role.ADMIN || user.role === User_Role.HOST);
};

/**
 * Formats an anonymous creator_id into a display-friendly label.
 * Handles patterns like:
 * - "anonymous_abc123" -> "Anonymous #abc123"
 * - "anonymous_abc123_John" -> "Anonymous (John)"
 * - "anonymous_abc123_John_Doe" -> "Anonymous (John_Doe)"
 */
export function formatAnonymousName(creatorId: string): string {
  if (!creatorId.startsWith("anonymous_")) {
    return creatorId; // Not an anonymous ID, return as-is
  }

  // Extract parts: anonymous_<sessionId>_<optionalName>
  const parts = creatorId.split("_");
  if (parts.length < 2) {
    return "Anonymous";
  }

  const sessionId = parts[1]; // The random ID part
  const displayName = parts.slice(2).join("_"); // Everything after sessionId

  if (displayName) {
    return `Anonymous (${displayName})`;
  } else {
    // Show last 4 chars of sessionId for identification
    const shortId = sessionId.length > 4 ? sessionId.slice(-4) : sessionId;
    return `Anonymous #${shortId}`;
  }
}
