import type { KPISet } from "@/lib/types";

export function computeRates(input: {
  total_emails_sent: number;
  total_opened: number;
  total_opens: number;
  total_clicked: number;
  total_clicks: number;
}): KPISet {
  const sent = input.total_emails_sent || 0;
  const opened = input.total_opened || 0;

  return {
    ...input,
    open_rate: sent > 0 ? opened / sent : 0,
    ctr: sent > 0 ? input.total_clicked / sent : 0,
    ctor: opened > 0 ? input.total_clicked / opened : 0,
  };
}

export function maxNumber(a: number, b: number) {
  return Math.max(a || 0, b || 0);
}

export function latestIso(a: string | null, b: string | null) {
  if (!a) return b;
  if (!b) return a;
  return a > b ? a : b;
}
