import type { Order } from "@/types/order";
import { PricingSummary } from "./pricing-summary";

/**
 * Renders a finished Order's pricing breakdown via the same PricingSummary
 * rows checkout uses — built from the columns CheckoutService persisted at
 * submit time (never recomputed), so a historical order keeps showing
 * exactly what it charged even if fee/tax config changes later. Percent
 * labels are derived from the stored amounts since Order doesn't persist
 * the rate itself (only the resulting fee), unlike the live /checkout/quote
 * response.
 */
export function OrderSummary({ order }: { order: Order }) {
  const discountedSubtotal = Number(order.subtotal) - Number(order.discount_amount);
  const percentOf = (amount: string) =>
    discountedSubtotal > 0 ? Math.round((Number(amount) / discountedSubtotal) * 100) : 0;

  const discountLabel = order.promo
    ? `Promo ${order.promo.code}`
    : order.voucher
      ? `Voucher ${order.voucher.code}`
      : "Voucher/Promo";

  return (
    <PricingSummary
      breakdown={{
        subtotal: order.subtotal,
        discount: order.discount_amount,
        discounted_subtotal: discountedSubtotal.toFixed(2),
        platform_fee: order.platform_fee,
        admin_fee: order.admin_fee,
        admin_fee_percent: percentOf(order.admin_fee),
        tax: order.tax_amount,
        tax_percent: percentOf(order.tax_amount),
        total: order.total_amount,
      }}
      discountLabel={discountLabel}
    />
  );
}
