export interface ImageModelVisibilityOption {
  value: string;
  label?: string;
}

export interface ImageModelVisibilitySession {
  user?: {
    role?: unknown;
    enterpriseRole?: unknown;
    isEnterpriseAdmin?: unknown;
  } | null;
}

function normalizeRole(value: unknown): string {
  return typeof value === "string" ? value.trim().toLowerCase() : "";
}

export function isVipImageModelOption(option: ImageModelVisibilityOption): boolean {
  const value = String(option.value || "").trim().toLowerCase();
  const label = String(option.label || "").trim().toLowerCase();
  return value.endsWith("-vip") || value.includes("-vip-") || /\bvip\b/.test(label);
}

export function isAdminImageModelUser(session: ImageModelVisibilitySession | null | undefined): boolean {
  const user = session?.user;
  if (!user) return false;
  return (
    normalizeRole(user.role) === "admin" ||
    normalizeRole(user.enterpriseRole) === "admin" ||
    user.isEnterpriseAdmin === true
  );
}

export function filterImageModelOptionsForSession<T extends ImageModelVisibilityOption>(
  options: T[],
  session: ImageModelVisibilitySession | null | undefined,
): T[] {
  if (isAdminImageModelUser(session)) return options;
  return options.filter((option) => !isVipImageModelOption(option));
}
