export type CashbackRecord = {
  id: string;
  batchNo: string;
  periodStart: string;
  periodEnd: string;
  confirmedAt: string | null;
  effectiveStake: string;
  betCount: number;
  rate: string;
  amount: string;
  createdAt: string;
};

/** API returns a plain array; older clients may expect { items }. */
export function parseCashbackApiData(data: unknown): CashbackRecord[] {
  if (Array.isArray(data)) return data as CashbackRecord[];
  if (data && typeof data === 'object' && 'items' in data) {
    const items = (data as { items?: unknown }).items;
    return Array.isArray(items) ? (items as CashbackRecord[]) : [];
  }
  return [];
}

export function sumCashbackAmount(rows: Array<{ amount: string }>): number {
  return rows.reduce((sum, row) => sum + Math.abs(parseFloat(row.amount) || 0), 0);
}
