/* ============================================================
   HOME — configurable hero · value strip · category grid ·
   featured products · one-cart band
   ============================================================ */

/* ── Hero — asymmetric media collage (dominant + supporting panels)
   above the headline+CTA, sized to exactly fill the first viewport
   together with the header ────────────────────────────────────── */
function HomeHero() {
  const { nav } = useStore();
  const H = BB.BRAND.hero;
  const panels = BB.IMG.hero.panels;
  const [main, ...rest] = panels;
  const side = rest.slice(0, 3);
  return (
    <section style={{ background: 'var(--cream)', borderBottom: '1px solid var(--line)', height: 'calc(100dvh - var(--header-h, 118px))', minHeight: 620, display: 'flex', flexDirection: 'column', overflow: 'hidden' }}>
      {/* media collage — dominant panel + supporting stack, full colour, no wash */}
      <div className="hero-media" style={{ flex: '1 1 auto', minHeight: 0, display: 'grid', gridTemplateColumns: 'clamp(220px,58%,760px) 1fr', gap: 'clamp(8px,1.2vw,14px)', padding: 'clamp(10px,1.6vh,20px) var(--gutter) 0' }}>
        <div className="hero-panel" style={{ position: 'relative', borderRadius: 'var(--r-lg)', overflow: 'hidden', boxShadow: 'var(--shadow-sm)', minHeight: 0 }}>
          <HeroPanel poster={main.img} video={main.vid} focus={main.focus} label={main.label} style={{ height: '100%' }} />
          <span className="hero-tag">{main.label}</span>
        </div>
        <div className="hero-side" style={{ display: 'grid', gridTemplateRows: `repeat(${side.length}, 1fr)`, gap: 'clamp(8px,1.2vw,14px)', minHeight: 0 }}>
          {side.map((p, i) => (
            <div key={i} className="hero-panel" style={{ position: 'relative', borderRadius: 'var(--r-md)', overflow: 'hidden', boxShadow: 'var(--shadow-sm)', minHeight: 0 }}>
              <HeroPanel poster={p.img} video={p.vid} focus={p.focus} label={p.label} style={{ height: '100%' }} />
              <span className="hero-tag hero-tag-sm">{p.label}</span>
            </div>
          ))}
        </div>
      </div>
      {/* headline + CTA — always below the media, never overlaid on it */}
      <div className="container wide" style={{ flex: '0 0 auto', padding: 'clamp(12px,2vh,26px) var(--gutter) clamp(16px,3vh,30px)' }}>
        <h1 className="display hero-headline" style={{ margin: '0 0 18px', animation: 'rise .7s .05s both' }}>
          {H.titleA}<br /><em style={{ color: 'var(--gold-deep)' }}>{H.titleB}</em>
        </h1>
        <div style={{ display: 'flex', gap: 12, flexWrap: 'wrap', animation: 'rise .7s .2s both' }}>
          <button className="btn btn-gold btn-lg" onClick={() => nav(H.cta.page)}>{H.cta.label} →</button>
          <button className="btn btn-outline btn-lg" onClick={() => nav(H.cta2.page)}>{H.cta2.label}</button>
        </div>
      </div>
    </section>
  );
}

/* ── Value strip — real platform features, no invented stats ── */
function ValueStrip() {
  const items = [
    [I.truck, 'Nationwide delivery', 'Every state, tracked door-to-door'],
    [I.shield, 'Secure checkout', 'Card, transfer — encrypted end to end'],
    [I.globe, 'Secure payment', 'Paystack, Flutterwave or wire transfer'],
    [I.box, 'Live order tracking', 'From confirmed to delivered'],
  ];
  return (
    <section style={{ borderBottom: '1px solid var(--line)', background: 'var(--ivory)' }}>
      <div className="container wide value-strip" style={{ display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 24, padding: '30px var(--gutter)' }}>
        {items.map(([Icon, t, s], i) =>
        <div key={i} style={{ display: 'flex', gap: 13, alignItems: 'center' }}>
            <span style={{ width: 44, height: 44, flexShrink: 0, borderRadius: 'var(--r-md)', background: 'var(--gold-soft)', display: 'grid', placeItems: 'center', color: 'var(--gold-deep)' }}>{Icon({ width: 21, height: 21 })}</span>
            <div style={{ lineHeight: 1.25 }}><div style={{ fontWeight: 600, fontSize: 14 }}>{t}</div><div style={{ fontSize: 12.5, color: 'var(--ink-soft)', marginTop: 2 }}>{s}</div></div>
          </div>
        )}
      </div>
    </section>);
}

/* ── Category grid ─────────────────────────────────────────── */
function CategoryGrid() {
  const { nav } = useStore();
  return (
    <section className="container wide" style={{ padding: 'clamp(60px,8vw,110px) var(--gutter)' }}>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', flexWrap: 'wrap', gap: 16, marginBottom: 38 }}>
        <div>
          <h2 style={{ fontSize: 'clamp(32px,4.5vw,54px)' }}>Very different goods.<br />One coherent store.</h2>
        </div>
        <button className="link-u" style={{ fontSize: 14, fontWeight: 600 }} onClick={() => nav('shop')}>View all products →</button>
      </div>
      <div className="cat-grid">
        {BB.navCategories.map((c, i) => {
          const count = BB.products.filter(p => BB.matchesNavCat(p, c)).length;
          const feature = c.id === 'vehicles';
          return (
          <CatTile key={c.id} c={c} i={i} count={count} feature={feature} nav={nav} />);
        })}
      </div>
    </section>);
}

function CatTile({ c, i, count, feature, nav }) {
  const [hover, setHover] = useState(false);
  return (
          <Reveal delay={i % 3 + 1} className="cat-tile" onClick={() => nav('shop', { cat: c.id })} style={{ cursor: 'pointer' }}
            onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)}>
            <Ph label={c.name} src={c.img} vid={c.vid} focus={c.focus} play={hover} style={{ height: '100%', minHeight: 210, borderRadius: 'var(--r-lg)' }} />
            <div style={{ position: 'absolute', inset: 0, borderRadius: 'var(--r-lg)', background: 'linear-gradient(to top, rgba(23,17,9,.85) 0%, rgba(23,17,9,.42) 45%, transparent 70%)', display: 'flex', flexDirection: 'column', justifyContent: 'flex-end', padding: 22, color: 'var(--cream)' }}>
              <h3 style={{ fontSize: 23, color: '#FFFDF8' }}>{c.name}</h3>
              {feature && <p style={{ fontSize: 13, color: 'rgba(255,253,248,.85)', marginTop: 6, maxWidth: 380 }}>{c.blurb}</p>}
              <span style={{ display: 'inline-flex', alignItems: 'center', gap: 7, fontSize: 12, letterSpacing: '.1em', textTransform: 'uppercase', marginTop: 10, color: 'var(--gold-soft)', fontWeight: 600 }}>{`${count} product${count !== 1 ? 's' : ''}`} <I.arrow width={15} height={15} /></span>
            </div>
          </Reveal>);
}

/* ── Featured products ─────────────────────────────────────── */
function Featured() {
  const { nav } = useStore();
  const [tab, setTab] = useState('best');
  const list = BB.products.filter((p) => tab === 'best' ? p.tags.includes('best') : p.tags.includes('new')).slice(0, 8);
  return (
    <section style={{ background: 'var(--cream-deep)', borderTop: '1px solid var(--line)', borderBottom: '1px solid var(--line)' }}>
      <div className="container wide" style={{ padding: 'clamp(60px,8vw,110px) var(--gutter)' }}>
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', flexWrap: 'wrap', gap: 18, marginBottom: 38 }}>
          <div>
            <h2 style={{ fontSize: 'clamp(32px,4.5vw,54px)' }}>Straight from the rail</h2>
          </div>
          <div style={{ display: 'flex', gap: 8 }}>
            <button className={`chip ${tab === 'best' ? 'active' : ''}`} onClick={() => setTab('best')}>Bestsellers</button>
            <button className={`chip ${tab === 'new' ? 'active' : ''}`} onClick={() => setTab('new')}>New in</button>
          </div>
        </div>
        <div className="prod-grid">
          {list.map((p, i) => <ProductCard key={p.id} p={p} delay={i % 4 + 1} />)}
        </div>
        <div style={{ textAlign: 'center', marginTop: 44 }}>
          <button className="btn btn-outline btn-lg" onClick={() => nav('shop')}>Shop everything</button>
        </div>
      </div>
    </section>);
}

function HomePage() {
  return (
    <>
      <HomeHero />
      <div className="stitch-divider" aria-hidden="true" />
      <ValueStrip />
      <CategoryGrid />
      <div className="stitch-divider" aria-hidden="true" />
      <Featured />
      <div className="stitch-divider" aria-hidden="true" />
      <Newsletter />
    </>);
}

Object.assign(window, { HomePage });
