// The lots and their pieces as a CSV the merchant opens in a spreadsheet. The column names stay in English: they are
// data, not interface text.
import { toCsv, decimal } from "./csv";
import type { CostedLot } from "./costing";

const HEADER = ["Lot", "Source", "Date", "Lot total", "Piece", "Units", "Cost per unit", "Piece total", "Selling price", "Cost in WooCommerce", "Status"];

export function lotsCsv(costed: CostedLot[]): string {
  const rows = costed.flatMap(({ lot, allocation, pieces }) =>
    pieces.map((piece) => [
      lot.name,
      lot.source,
      lot.purchasedAt,
      decimal(allocation.totalCents),
      piece.title,
      piece.units,
      piece.unitCostCents === null ? null : decimal(piece.unitCostCents),
      decimal(piece.totalCents),
      piece.priceCents === null ? null : decimal(piece.priceCents),
      piece.storeCostCents === null ? null : decimal(piece.storeCostCents),
      piece.status,
    ]),
  );
  return toCsv(HEADER, rows);
}
