// Shared constants and helpers for CustomerDetail sub-components

export const ORDER_STATUS_LABELS = {
  negotiating: "Negotiating",
  awaiting_quotation: "Awaiting Quotation",
  awaiting_customer_confirmation: "Awaiting Confirmation",
  awaiting_fulfilment: "Awaiting Fulfilment",
  awaiting_payment: "Awaiting Payment",
  manufacturing: "Manufacturing",
  shipped: "Shipped",
  installed: "Installed",
  declined: "Declined",
  complete: "Complete",
};

export const ORDER_STATUS_STYLES = {
  negotiating:                      { bg: "var(--crm-ord-negotiating-bg)",       color: "var(--crm-ord-negotiating-text)" },
  awaiting_quotation:               { bg: "var(--crm-ord-awaiting_quotation-bg)",color: "var(--crm-ord-awaiting_quotation-text)" },
  awaiting_customer_confirmation:   { bg: "var(--crm-ord-awaiting_customer_confirmation-bg)", color: "var(--crm-ord-awaiting_customer_confirmation-text)" },
  awaiting_fulfilment:              { bg: "var(--crm-ord-awaiting_fulfilment-bg)",color: "var(--crm-ord-awaiting_fulfilment-text)" },
  awaiting_payment:                 { bg: "var(--crm-ord-awaiting_payment-bg)",  color: "var(--crm-ord-awaiting_payment-text)" },
  manufacturing:                    { bg: "var(--crm-ord-manufacturing-bg)",      color: "var(--crm-ord-manufacturing-text)" },
  shipped:                          { bg: "var(--crm-ord-shipped-bg)",            color: "var(--crm-ord-shipped-text)" },
  installed:                        { bg: "var(--crm-ord-installed-bg)",          color: "var(--crm-ord-installed-text)" },
  declined:                         { bg: "var(--crm-ord-declined-bg)",           color: "var(--crm-ord-declined-text)" },
  complete:                         { bg: "var(--crm-ord-complete-bg)",           color: "var(--crm-ord-complete-text)" },
};

export const REL_STATUS_LABELS = {
  lead:     "Lead",
  prospect: "Prospect",
  active:   "Active",
  inactive: "Inactive",
  churned:  "Churned",
};

export const REL_STATUS_STYLES = {
  lead:     { bg: "var(--crm-rel-lead-bg)",     color: "var(--crm-rel-lead-text)",     border: "var(--crm-rel-lead-border)" },
  prospect: { bg: "var(--crm-rel-prospect-bg)", color: "var(--crm-rel-prospect-text)", border: "var(--crm-rel-prospect-border)" },
  active:   { bg: "var(--crm-rel-active-bg)",   color: "var(--crm-rel-active-text)",   border: "var(--crm-rel-active-border)" },
  inactive: { bg: "var(--crm-rel-inactive-bg)", color: "var(--crm-rel-inactive-text)", border: "var(--crm-rel-inactive-border)" },
  churned:  { bg: "var(--crm-rel-churned-bg)",  color: "var(--crm-rel-churned-text)",  border: "var(--crm-rel-churned-border)" },
};

export const TIMELINE_TYPE_LABELS = {
  negotiations_started: "Started Negotiations",
  quote_request:        "Quote Requested",
  quote_sent:           "Quote Sent",
  quote_accepted:       "Quote Accepted",
  quote_declined:       "Quote Declined",
  mfg_started:          "Manufacturing Started",
  mfg_complete:         "Manufacturing Complete",
  order_shipped:        "Order Shipped",
  installed:            "Installed",
  payment_received:     "Payment Received",
  invoice_sent:         "Invoice Sent",
  note:                 "Note",
};

export const PAYMENT_TYPE_LABELS = {
  cash:          "Cash",
  bank_transfer: "Bank Transfer",
  card:          "Card",
  paypal:        "PayPal",
};

export const FLOW_LABELS = {
  invoice: "Invoice",
  payment: "Payment",
  refund:  "Refund",
  credit:  "Credit",
};

export const CATEGORY_LABELS = {
  full_payment:  "Full Payment",
  advance:       "Advance",
  installment:   "Installment",
};

export function fmtDate(iso) {
  if (!iso) return "—";
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return "—";
  return d.toLocaleDateString("en-GB", { day: "2-digit", month: "short", year: "2-digit" });
}

export function fmtDateTime(iso) {
  if (!iso) return "—";
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return "—";
  return d.toLocaleDateString("en-GB", { day: "2-digit", month: "short", year: "2-digit" })
    + " " + d.toLocaleTimeString("en-US", { hour: "numeric", minute: "2-digit" });
}

export function fmtDateFull(iso) {
  if (!iso) return "—";
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return "—";
  return d.toLocaleDateString("en-GB", { day: "2-digit", month: "long", year: "numeric" });
}

export function OrderStatusChip({ status, style }) {
  const s = ORDER_STATUS_STYLES[status] || { bg: "var(--bg-card-hover)", color: "var(--text-secondary)" };
  return (
    <span style={{
      display: "inline-block",
      padding: "2px 8px",
      borderRadius: 12,
      fontSize: 11,
      fontWeight: 600,
      backgroundColor: s.bg,
      color: s.color,
      ...style,
    }}>
      {ORDER_STATUS_LABELS[status] || status}
    </span>
  );
}

export function RelStatusChip({ status, style }) {
  const s = REL_STATUS_STYLES[status] || { bg: "var(--bg-card-hover)", color: "var(--text-secondary)", border: "var(--border-primary)" };
  return (
    <span style={{
      display: "inline-block",
      padding: "2px 10px",
      borderRadius: 12,
      fontSize: 11,
      fontWeight: 600,
      backgroundColor: s.bg,
      color: s.color,
      border: `1px solid ${s.border}`,
      ...style,
    }}>
      {REL_STATUS_LABELS[status] || status}
    </span>
  );
}
