// Who may change things. Shared by server actions (the real check) and pages,
// which only use it to leave out buttons a viewer couldn't use anyway.

export type UserRole = "ADMIN" | "VIEWER";

export const ROLE_LABELS: Record<UserRole, string> = { ADMIN: "Super Admin", VIEWER: "View only" };

export const VIEW_ONLY_ERROR = "Your account is view-only. Ask an administrator to make this change.";

/** Add / edit / delete, starting checks by hand, and Monitoring Settings. */
export function canEdit(user: { role: UserRole } | null | undefined): boolean {
  return user?.role === "ADMIN";
}

/** `--role viewer` on the command line → "VIEWER"; null for anything else. */
export function parseRole(input: string | undefined): UserRole | null {
  const value = input?.trim().toUpperCase();
  if (value === "ADMIN" || value === "SUPER-ADMIN" || value === "SUPERADMIN") return "ADMIN";
  if (value === "VIEWER" || value === "VIEW-ONLY" || value === "READONLY" || value === "READ-ONLY") return "VIEWER";
  return null;
}
