import type { PurchaseContext } from "@/types";

export type CheckoutTotals = {
  subtotal: number;
  discount: number;
  fee: number;
  total: number;
};

export function computeTotals(ctx: PurchaseContext, discount = 0): CheckoutTotals {
  const subtotal = +(ctx.price * (ctx.quantity ?? 1)).toFixed(2);
  const fee = ctx.purchaseType === "marketplace" ? +(subtotal * 0.05).toFixed(2) : 0;
  const total = Math.max(0, +(subtotal - discount + fee).toFixed(2));
  return { subtotal, discount, fee, total };
}

export const checkoutService = {
  async submit(ctx: PurchaseContext, opts: { promoCode?: string; paymentMethod: string; recipientId?: string }) {
    await new Promise((r) => setTimeout(r, 1200));
    return { ok: true as const, orderId: `ord-${Date.now()}`, ctx, opts };
  },
};