/** Helpers used at enqueue time to pre-format complex HTML fragments. */
import { escapeHtml } from './render';

export interface OrderItem { name: string; quantity: number; price?: number }

export function formatMoney(value: number): string {
  return `$${value.toFixed(2)}`;
}

/** Render the per-component totals rows used by `order_new_customer`.
 *  Returns one `<tr>` row per non-zero line so the email matches the
 *  dashboard order detail (subtotal → coupon → loyalty → gift card → tax). */
export function formatOrderTotalRows(o: {
  subtotal: number;
  couponCode?: string | null;
  discountAmount?: number;
  loyaltyPointsRedeemed?: number;
  loyaltyDiscount?: number;
  giftCardApplied?: number;
  tax: number;
}): {
  subtotalRowHtml: string;
  couponRowHtml: string;
  loyaltyRowHtml: string;
  giftCardRowHtml: string;
  taxRowHtml: string;
} {
  const row = (label: string, amount: string, negative = false) =>
    `<tr><td style="padding:6px 0;color:${negative ? '#047857' : '#374151'};">${escapeHtml(label)}</td><td style="padding:6px 0;color:${negative ? '#047857' : '#111827'};text-align:right;font-weight:600;">${negative ? '−' : ''}${escapeHtml(amount)}</td></tr>`;
  const discount = o.discountAmount ?? 0;
  const loyalty = o.loyaltyDiscount ?? 0;
  const gift = o.giftCardApplied ?? 0;
  return {
    subtotalRowHtml: row('Subtotal', formatMoney(o.subtotal)),
    couponRowHtml: discount > 0
      ? row(`Coupon${o.couponCode ? ` (${o.couponCode})` : ''}`, formatMoney(discount), true)
      : '',
    loyaltyRowHtml: loyalty > 0
      ? row(`Loyalty points${o.loyaltyPointsRedeemed ? ` (${o.loyaltyPointsRedeemed} pts)` : ''}`, formatMoney(loyalty), true)
      : '',
    giftCardRowHtml: gift > 0
      ? row('Gift card', formatMoney(gift), true)
      : '',
    taxRowHtml: row('Tax', formatMoney(o.tax)),
  };
}

export function formatOrderItemsHtml(items: OrderItem[]): string {
  if (!items?.length) return '';
  const rows = items.map(i => {
    const sub = i.price ? formatMoney(i.price * i.quantity) : '';
    return `<tr>
      <td style="padding:8px 0;color:#374151;font-size:14px;">${escapeHtml(i.quantity + 'x ' + i.name)}</td>
      <td style="padding:8px 0;color:#111827;font-size:14px;text-align:right;font-weight:600;">${sub}</td>
    </tr>`;
  }).join('');
  return `<table cellpadding="0" cellspacing="0" style="width:100%;border-top:1px solid #e5e7eb;border-bottom:1px solid #e5e7eb;">${rows}</table>`;
}

export function formatOrderSummaryHtml(o: { ref: string; customer: string; total: number; itemsCount: number }): string {
  return `<div style="margin:8px 0;padding:12px;background:#f9fafb;border-radius:10px;">
    <div style="font-size:13px;color:#6b7280;">${escapeHtml(o.ref)} · ${escapeHtml(o.customer)}</div>
    <div style="font-size:14px;color:#111827;">${o.itemsCount} item(s) · ${formatMoney(o.total)}</div>
  </div>`;
}

export function formatBookingSummaryHtml(b: { ref: string; guest: string; date: string; time: string; partySize: number }): string {
  return `<div style="margin:8px 0;padding:12px;background:#f9fafb;border-radius:10px;">
    <div style="font-size:13px;color:#6b7280;">${escapeHtml(b.ref)} · ${escapeHtml(b.guest)}</div>
    <div style="font-size:14px;color:#111827;">${escapeHtml(b.date)} ${escapeHtml(b.time)} · ${b.partySize} guests</div>
  </div>`;
}
