/* ============================================================================
 * components.jsx — shared UI building blocks
 * ----------------------------------------------------------------------------
 * Lightweight pieces used across views: Drawer, Field, Select, PhoneInput,
 * Pill, EmptyState, Spinner. Every component is purely presentational —
 * data fetching lives inside the views.
 * ========================================================================= */

function Drawer({ title, onClose, children, footer, width }) {
  return (
    <div className="drawer-mask" onClick={(e) => { if (e.target.classList.contains("drawer-mask")) onClose(); }}>
      <div className="drawer" style={width ? { width } : null}>
        <div className="drawer-head">
          <h3>{title}</h3>
          <button className="icon-btn" onClick={onClose} aria-label="Close">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>
          </button>
        </div>
        <div className="drawer-body">{children}</div>
        {footer && <div className="drawer-foot">{footer}</div>}
      </div>
    </div>
  );
}

function Field({ label, help, error, children, required }) {
  return (
    <div className="form-row">
      <label>{label}{required ? " *" : ""}</label>
      {children}
      {help && !error && <div className="field-help">{help}</div>}
      {error && <div className="field-error">{error}</div>}
    </div>
  );
}

/* Dropdown that takes a flat array of snake_case values and renders them
 * with humanize() — so the visible label is "Out for Delivery" while the
 * stored value remains "out_for_delivery". */
function EnumSelect({ value, onChange, options, placeholder, allowEmpty }) {
  return (
    <select className="select" value={value || ""} onChange={(e) => onChange(e.target.value)}>
      {allowEmpty && <option value="">{placeholder || "— Select —"}</option>}
      {options.map(opt => <option key={opt} value={opt}>{humanize(opt)}</option>)}
    </select>
  );
}

/* Phone input — splits country code from local number, validates on blur. */
function PhoneInput({ value, onChange, error }) {
  const cc = (window.WYF_CONFIG && window.WYF_CONFIG.DEFAULT_COUNTRY_CODE) || "+63";
  // strip leading cc for display
  let local = value || "";
  if (local.startsWith(cc)) local = local.slice(cc.length);
  return (
    <>
      <div className="phone-wrap">
        <span className="cc">{cc}</span>
        <input className={"input" + (error ? " error" : "")} type="tel"
               placeholder="9XX XXX XXXX"
               value={local}
               onChange={(e) => onChange(e.target.value)} />
      </div>
      {error && <div className="field-error">{error}</div>}
    </>
  );
}

function Pill({ children, tone }) {
  return <span className={"pill " + (tone || "")}>{children}</span>;
}

/* Tone for an order status — keeps coloring consistent between dashboard +
 * orders table. If you add a new status, add it here too. */
function statusTone(status) {
  switch (status) {
    case "completed":
    case "delivered":
    case "paid":
      return "success";
    case "cancelled":
    case "unpaid":
      return "danger";
    case "preparing":
    case "out_for_delivery":
    case "ready":
    case "partial":
      return "warn";
    default:
      return "info";
  }
}

function EmptyState({ title, hint }) {
  return <div className="empty"><div style={{ fontWeight: 600, marginBottom: 4 }}>{title}</div><div className="muted">{hint}</div></div>;
}

function Spinner({ label }) {
  return (
    <div style={{ padding: 24, textAlign: "center", color: "var(--text-muted)", fontSize: 12 }}>
      <div style={{ width: 18, height: 18, border: "2px solid var(--border-strong)", borderTopColor: "var(--brand-2)", borderRadius: "50%", animation: "spin 0.8s linear infinite", display: "inline-block", marginRight: 8, verticalAlign: "middle" }} />
      {label || "Loading…"}
      <style>{`@keyframes spin { to { transform: rotate(360deg); } }`}</style>
    </div>
  );
}

/* Sidebar nav item with icon */
function NavItem({ icon, label, active, onClick }) {
  return (
    <button className={"nav-item" + (active ? " active" : "")} onClick={onClick}>
      {icon}<span>{label}</span>
    </button>
  );
}

/* SVG icon set kept inline — no icon-font dependency. */
const Icons = {
  home: (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M3 11.5 12 4l9 7.5"/><path d="M5 10v10h14V10"/></svg>),
  contacts: (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><circle cx="12" cy="8" r="3.5"/><path d="M5 20c1.5-3.5 4-5 7-5s5.5 1.5 7 5"/></svg>),
  orders: (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M5 7h14l-1.5 11h-11z"/><path d="M9 7V5a3 3 0 0 1 6 0v2"/></svg>),
  products: (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="m12 3 8 4v10l-8 4-8-4V7z"/><path d="m4 7 8 4 8-4M12 11v10"/></svg>),
  expenses: (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><rect x="3" y="6" width="18" height="13" rx="2"/><path d="M3 10h18M7 15h3"/></svg>),
  production: (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M4 19V9l4 3 4-6 4 6 4-3v10z"/></svg>),
  inventory: (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><rect x="4" y="6" width="16" height="14" rx="1"/><path d="M4 10h16M9 6V4h6v2"/></svg>),
  users: (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><circle cx="9" cy="9" r="3"/><path d="M3 19c1.2-3 3.5-4.5 6-4.5s4.8 1.5 6 4.5"/><circle cx="17" cy="8" r="2.5"/><path d="M15 14c2 0 4 1.2 5 3"/></svg>),
  sun: (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M2 12h2M20 12h2M5 5l1.5 1.5M17.5 17.5 19 19M5 19l1.5-1.5M17.5 6.5 19 5"/></svg>),
  moon: (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M21 13A9 9 0 1 1 11 3a7 7 0 0 0 10 10z"/></svg>),
  logout: (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M15 4h4v16h-4M10 8l-4 4 4 4M6 12h10"/></svg>),
  add: (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>),
};

Object.assign(window, { Drawer, Field, EnumSelect, PhoneInput, Pill, statusTone, EmptyState, Spinner, NavItem, Icons });
