/* ============================================================
   PURE SPA — AGENDA · utilidades de datos para vistas admin
   ============================================================ */
function custName(bk) {
  if (!bk) return "—";
  if (bk._customerName) return bk._customerName;
  const c = PureApi.getCustomerById(bk.customerId);
  return c ? c.name : "Sin asignar";
}
function custPhone(bk) {
  if (!bk) return "—";
  if (bk._customerPhone) return bk._customerPhone;
  const c = PureApi.getCustomerById(bk.customerId);
  return c ? c.phone : "—";
}
function custEmail(bk) {
  if (bk._customerEmail) return bk._customerEmail;
  const c = PureApi.getCustomerById(bk.customerId);
  return c ? c.email : "—";
}
function svcOf(bk) { return PureApi.getServiceById(bk.serviceId); }
function bedName(id) { const b = PureApi.getBeds().find((x) => x.id === id); return b ? b.name : "Sin camilla"; }
function bedShort(id) { const b = PureApi.getBeds().find((x) => x.id === id); return b ? b.code : "—"; }
function initials(name) { return (name || "?").split(" ").map((w) => w[0]).slice(0, 2).join("").toUpperCase(); }
function todayChileIso() {
  const parts = new Intl.DateTimeFormat("en-CA", {
    timeZone: "America/Santiago",
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
  }).formatToParts(new Date());
  const byType = Object.fromEntries(parts.map((p) => [p.type, p.value]));
  return `${byType.year}-${byType.month}-${byType.day}`;
}
const TODAY = todayChileIso();

/* avatar redondo con iniciales */
function Avatar({ name, size = 38, tone = "dark" }) {
  const bg = tone === "dark" ? C.espresso : C.champagne;
  const fg = tone === "dark" ? C.gold : C.goldDeep;
  return (
    <div style={{ width: size, height: size, borderRadius: 999, background: bg, color: fg, flexShrink: 0,
      display: "flex", alignItems: "center", justifyContent: "center", fontFamily: "var(--font-display)", fontSize: size * 0.42 }}>
      {initials(name)}
    </div>
  );
}

/* tabla → cards responsive helper: contenedor con overflow */
function TableWrap({ children }) {
  return <div style={{ overflowX: "auto", borderRadius: 16, border: `1px solid ${C.line}`, background: C.white,
    boxShadow: "0 2px 16px rgba(43,37,32,0.05)" }}>{children}</div>;
}

Object.assign(window, { custName, custPhone, custEmail, svcOf, bedName, bedShort, initials, Avatar, TableWrap, TODAY });
