import type { CartItem, Order } from "./types";

export const FREE_SHIPPING_CENTS = 9900, STD_SHIPPING = 1290, EXPRESS_SHIPPING = 1990, PIX_DISCOUNT = 0.05;
export const COUPONS: Record<string, number> = { BEMVINDO: 0.1 };
export interface Totals { sub: number; dc: number; ship: number; tot: number; pix: number }

export function totals(lines: { priceCents: number; q: number }[], opts: { coupon?: string; ship?: number } = {}): Totals {
  const sub = lines.reduce((a, l) => a + l.priceCents * l.q, 0);
  const base = !sub || sub >= FREE_SHIPPING_CENTS ? 0 : STD_SHIPPING;
  const ship = opts.ship ?? base;
  const rate = opts.coupon ? COUPONS[opts.coupon.toUpperCase()] ?? 0 : 0;
  const dc = Math.round(sub * rate);
  return { sub, dc, ship, tot: sub - dc + ship, pix: Math.round((sub - dc) * (1 - PIX_DISCOUNT)) + ship };
}
export const ORDER_STEPS = ["Pedido confirmado", "Em separação", "Enviado", "Entregue"];
export function newOrderId() { return "SB" + Math.floor(10000000 + Math.random() * 89999999); }
export function createOrder(items: CartItem[], total: number, pay: string): Order {
  return { id: newOrderId(), date: new Date().toLocaleDateString("pt-BR"), status: 0, items: items.map((i) => ({ ...i })), total, pay };
}
export const SEED_ORDERS: Order[] = [{ id: "SB48201937", date: "02/10/2026", status: 3, items: [{ id: 10, q: 1 }, { id: 8, q: 1 }], total: 20980, pay: "Pix" }];
