import type { CartItem } from "./cart";

// sessionStorage, not localStorage — this is a one-time receipt for the
// tab that just placed the order, not something that should persist
// forever. Written by /checkout's "Jetzt kaufen" submit once
// POST /api/checkout confirms the order was actually persisted in
// Payload (orderNumber/orderDateIso come back from that response, not
// generated locally), read once by /bestellbestaetigung.
export const ORDER_KEY = "ep_last_order";

// Written for a gated payment method (Kreditkarte/PayPal) right before
// PaymentStep hands off to Stripe/the test-confirm flow — see
// spicy-leaping-pizza.md §3/§7. Same OrderSnapshot shape as ORDER_KEY,
// but this one is provisional: /checkout/verarbeitung only promotes it
// to ORDER_KEY once polling confirms the payment actually succeeded, so
// an abandoned/failed payment never leaves a confirmation-page-ready
// snapshot behind.
export const PENDING_ORDER_KEY = "ep_pending_order";

export type OrderSnapshot = {
  items: CartItem[];
  orderNumber: string;
  orderDateIso: string;
  shippingCost: number;
  paymentMethodTitle: string;
  /** Persisted (not re-derived) so /bestellbestaetigung shows exactly what
   * was actually applied at purchase time, not a fresh re-validation —
   * null/0 when no discount was ever applied. */
  discountCode: string | null;
  discountAmount: number;
  /** Decided server-side at checkout (live VIES check, see api/checkout/
   * route.ts) — /bestellbestaetigung needs this to know whether to show
   * the exempt (net, de-grossed) totals instead of the normal VAT-
   * inclusive catalog prices it would otherwise re-derive live. */
  vatExempt: boolean;
  /** §19 UStG — this tenant's company-settings.kleinunternehmer as it stood
   * at checkout time (see api/checkout/route.ts), never re-derived live —
   * takes precedence over vatExempt above wherever both would otherwise
   * apply. /bestellbestaetigung uses this to show the §19 notice instead
   * of a per-item "inkl. X% MwSt." hint/VAT breakdown. */
  kleinunternehmer: boolean;
};
