// ui.jsx — shared contexts + UI primitives

const LangCtx = React.createContext({ lang: 'es', t: STR.es });
const CartCtx = React.createContext(null);
const NavCtx  = React.createContext(null);
const useLang = () => React.useContext(LangCtx);
const useCart = () => React.useContext(CartCtx);
const useNav  = () => React.useContext(NavCtx);

// Add (+) button that turns into a qty stepper once in cart
function QtyControl({ id, size = 'md' }) {
  const cart = useCart();
  const q = cart.qty(id);
  if (q === 0) {
    return (
      <button className="add-btn" onClick={() => cart.add(id)} aria-label="Agregar">
        <Icon name="plus" size={20} sw={2.2} />
      </button>
    );
  }
  return (
    <div className="qty">
      <button onClick={() => cart.remove(id)} aria-label="Quitar"><Icon name="minus" size={16} sw={2.4} /></button>
      <span>{q}</span>
      <button onClick={() => cart.add(id)} aria-label="Sumar"><Icon name="plus" size={16} sw={2.4} /></button>
    </div>
  );
}

// Full-width product row card
function ProductCard({ p }) {
  const { lang } = useLang();
  return (
    <div className="card" style={{ display: 'flex', gap: 13, padding: 11, alignItems: 'center' }}>
      <Placeholder icon={p.icon} tone={p.tone} style={{ width: 76, height: 76, flexShrink: 0, borderRadius: 'var(--r-sm)' }} />
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 7, flexWrap: 'wrap' }}>
          <span style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 15.5, color: 'var(--c-ink)', lineHeight: 1.15 }}>
            {lang === 'en' && p.en ? p.en : p.name}
          </span>
          {p.tag && <span className="tag">{p.tag}</span>}
        </div>
        <p style={{ margin: '3px 0 8px', fontSize: 12.5, color: 'var(--c-ink-soft)', lineHeight: 1.4 }}>{p.desc}</p>
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
          <span style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 16, color: 'var(--c-accent-ink)' }}>{fmt(p.price)}</span>
          <QtyControl id={p.id} />
        </div>
      </div>
    </div>
  );
}

// Small card for horizontal "featured" rail
function ProductMini({ p }) {
  const { lang } = useLang();
  return (
    <div className="card" style={{ width: 156, flexShrink: 0, overflow: 'hidden' }}>
      <Placeholder icon={p.icon} tone={p.tone} style={{ height: 104, borderRadius: 0 }} />
      <div style={{ padding: '10px 11px 11px' }}>
        <div style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 14, lineHeight: 1.15, minHeight: 32 }}>
          {lang === 'en' && p.en ? p.en : p.name}
        </div>
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginTop: 8 }}>
          <span style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 14.5, color: 'var(--c-accent-ink)' }}>{fmt(p.price)}</span>
          <QtyControl id={p.id} />
        </div>
      </div>
    </div>
  );
}

// Ticket-style combo card
function ComboCard({ c }) {
  const cart = useCart();
  const { t } = useLang();
  return (
    <div className="ticket" style={{ display: 'flex' }}>
      <Placeholder icon={c.icon} tone={c.tone} label={null} style={{ width: 92, borderRadius: 0, flexShrink: 0 }} />
      <div className="perf" style={{ left: 92 }} />
      <div style={{ flex: 1, padding: '13px 14px 13px 18px', minWidth: 0 }}>
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', gap: 8 }}>
          <div>
            <div style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 16 }}>{c.name}</div>
            <div style={{ fontSize: 11.5, color: 'var(--c-ink-soft)', marginTop: 1 }}>{c.for}</div>
          </div>
          {c.tag && <span className="tag" style={{ flexShrink: 0 }}>{c.tag}</span>}
        </div>
        <ul style={{ margin: '9px 0 11px', padding: 0, listStyle: 'none', display: 'flex', flexDirection: 'column', gap: 3 }}>
          {c.items.map((it, i) => (
            <li key={i} style={{ fontSize: 12.5, color: 'var(--c-ink)', display: 'flex', alignItems: 'center', gap: 7 }}>
              <span style={{ color: 'var(--c-brand)', display: 'flex' }}><Icon name="check" size={13} sw={2.4} /></span>{it}
            </li>
          ))}
        </ul>
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
          <span style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 18, color: 'var(--c-accent-ink)' }}>{fmt(c.price)}</span>
          <button className="btn btn-dark" style={{ padding: '9px 16px', fontSize: 13.5 }} onClick={() => cart.add(c.id)}>
            <Icon name="plus" size={16} sw={2.4} /> {t.addCombo}
          </button>
        </div>
      </div>
    </div>
  );
}

function SectionHead({ eyebrow, title, sub, action }) {
  return (
    <div style={{ display: 'flex', alignItems: 'flex-end', justifyContent: 'space-between', gap: 12, margin: '0 0 13px' }}>
      <div>
        {eyebrow && <div className="h-eyebrow" style={{ marginBottom: 5 }}>{eyebrow}</div>}
        <div className="h-title">{title}</div>
        {sub && <div className="h-sub" style={{ marginTop: 3 }}>{sub}</div>}
      </div>
      {action}
    </div>
  );
}

// Top app header
function Header() {
  const { lang, setLang, t } = useLang();
  return (
    <div style={{ position: 'relative', zIndex: 2, background: 'var(--c-surface-2)', borderBottom: '1px solid var(--c-line)', padding: 'var(--top-pad) 18px 12px' }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 11 }}>
        <Stamp size={42} />
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontFamily: 'var(--font-display)', fontWeight: 800, fontSize: 17, letterSpacing: '-.01em', lineHeight: 1 }}>Cafetería La Z</div>
          <div style={{ display: 'flex', alignItems: 'center', gap: 5, marginTop: 3, fontSize: 11.5, color: 'var(--c-ink-soft)' }}>
            <Icon name="pin" size={13} sw={1.8} style={{ color: 'var(--c-accent)' }} />{t.terminal}
          </div>
        </div>
        <button onClick={() => setLang(lang === 'es' ? 'en' : 'es')}
          style={{ display: 'flex', alignItems: 'center', gap: 5, background: 'var(--c-bg)', border: '1px solid var(--c-line)', borderRadius: 999, padding: '7px 11px', cursor: 'pointer', color: 'var(--c-ink)', fontWeight: 700, fontSize: 12, fontFamily: 'var(--font-body)' }}>
          <Icon name="globe" size={14} sw={1.8} />{lang.toUpperCase()}
        </button>
      </div>
    </div>
  );
}

// Open-now status strip
function OpenStrip() {
  const { t } = useLang();
  return (
    <div style={{ display: 'inline-flex', alignItems: 'center', gap: 7, fontSize: 12.5, fontWeight: 600, color: 'var(--c-brand)', whiteSpace: 'nowrap' }}>
      <span style={{ width: 8, height: 8, borderRadius: 999, background: '#2e9e5b', boxShadow: '0 0 0 3px rgba(46,158,91,.18)', flexShrink: 0 }} />
      {t.open} · <span style={{ color: 'var(--c-ink-soft)', fontWeight: 500 }}>{t.closes}</span>
    </div>
  );
}

// Bottom tab bar
function TabBar() {
  const { tab, go } = useNav();
  const { t } = useLang();
  const tabs = [
    { id: 'home', icon: 'coffee' },
    { id: 'menu', icon: 'sandwich' },
    { id: 'esquel', icon: 'mountain' },
    { id: 'place', icon: 'pin' },
  ];
  return (
    <div className="tabbar">
      {tabs.map((tb, i) => (
        <button key={tb.id} className={tab === tb.id ? 'is-on' : ''} onClick={() => go(tb.id)}>
          <Icon name={tb.icon} size={23} sw={tab === tb.id ? 2.1 : 1.7} />
          <span className="tab-lbl">{t.tabs[i]}</span>
        </button>
      ))}
    </div>
  );
}

// Floating "view my order" bar
function CartBar() {
  const cart = useCart();
  const { go } = useNav();
  const { t } = useLang();
  if (cart.count === 0) return null;
  return (
    <div style={{ position: 'absolute', left: 14, right: 14, bottom: 80, zIndex: 4 }}>
      <button className="btn btn-primary btn-block" style={{ justifyContent: 'space-between', padding: '15px 18px', boxShadow: '0 10px 26px rgba(160,74,32,.4)' }} onClick={() => go('cart')}>
        <span style={{ display: 'flex', alignItems: 'center', gap: 9 }}>
          <span style={{ background: 'rgba(255,255,255,.22)', borderRadius: 999, minWidth: 24, height: 24, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontSize: 13, fontWeight: 700 }}>{cart.count}</span>
          {t.viewOrder}
        </span>
        <span style={{ display: 'flex', alignItems: 'center', gap: 8, fontWeight: 700 }}>{fmt(cart.total)} <Icon name="arrowR" size={18} sw={2.2} /></span>
      </button>
    </div>
  );
}

Object.assign(window, {
  LangCtx, CartCtx, NavCtx, useLang, useCart, useNav,
  QtyControl, ProductCard, ProductMini, ComboCard, SectionHead,
  Header, OpenStrip, TabBar, CartBar,
});
