export type PlatformMatch = {
  type?: string;
  value?: string;
  sourceUserId?: string | null;
  sourceEmail?: string | null;
};

export function matchTypeLabel(type?: string) {
  if (type === "ip") return "IP address";
  if (type === "deviceToken") return "Device token";
  if (type === "deviceFingerprint") return "Device fingerprint";
  return type || "Unknown signal";
}

/** Compact labels for dense table cells */
export function shortMatchTypeLabel(type?: string) {
  if (type === "ip") return "IP";
  if (type === "deviceToken") return "Device";
  if (type === "deviceFingerprint") return "Fingerprint";
  return matchTypeLabel(type);
}

export function truncateSignalValue(value?: string | null, max = 28) {
  const text = (value || "").trim();
  if (!text) return "—";
  if (text.length <= max) return text;
  const head = Math.max(10, Math.floor(max * 0.55));
  const tail = Math.max(6, max - head - 1);
  return `${text.slice(0, head)}…${text.slice(-tail)}`;
}

export function formatMatchLine(match: PlatformMatch) {
  const label = matchTypeLabel(match.type);
  const value = match.value || "—";
  const source = match.sourceEmail ? ` (from ${match.sourceEmail})` : "";
  return `${label}: ${value}${source}`;
}

export function formatMatchSummary(matches?: PlatformMatch[] | null) {
  if (!matches?.length) return "Matching platform-suspension flag";
  return matches.map(formatMatchLine).join("; ");
}
