/* ============================================================
   CHINASA MARTINS — Homepage
   ============================================================ */

function Hero({ onNav, headline, sub }) {
  return <HeroCollage onNav={onNav} headline={headline} sub={sub} />;
}

/* ---------------- TRUST BAR ---------------- */
function TrustBar() {
  const items = [
    { icon: "scissors", t: "Ready-to-wear", s: "Statement pieces, limited runs" },
    { icon: "ig", t: (window.BRAND && window.BRAND.igHandle) || "@nasamartins", s: "New looks & drops on Instagram" },
    { icon: "mail", t: "Enquire for pricing", s: "DM or use the enquiry form" },
    { icon: "shield", t: "Secure checkout", s: "On-site enquiry, no spam" },
  ];
  return (
    <div className="trustbar">
      <div className="wrap trustbar-inner">
        {items.map((it) => (
          <div className="trust-item" key={it.t}>
            <Icon name={it.icon} size={22} stroke={1.4} />
            <div><span className="trust-t">{it.t}</span><span className="trust-s">{it.s}</span></div>
          </div>
        ))}
      </div>
    </div>
  );
}

/* ---------------- CATEGORY STRIP ---------------- */
function CategoryStrip({ onNav }) {
  const cats = [
    { name: "Tailoring", label: "LOOK · NEON BLAZER SET", n: "01" },
    { name: "Eveningwear", label: "LOOK · RUFFLE CAPE GOWN", n: "02" },
    { name: "Two-Piece Sets", label: "LOOK · RED CARGO SET", n: "03" },
    { name: "Denim", label: "LOOK · DENIM JACKET & SKIRT", n: "04" },
  ];
  return (
    <section className="section-pad-sm">
      <div className="wrap-wide">
        <Reveal className="section-head" style={{ marginBottom: 40 }}>
          <h2 className="serif">Shop by category</h2>
          <button className="link-arrow" onClick={() => onNav("shop", {})}>All products</button>
        </Reveal>
        <div className="cat-strip">
          {cats.map((c, i) => (
            <Reveal key={c.name} delay={(i % 4) + 1} className="cat-card zoomable">
              <button className="cat-card-btn" onClick={() => onNav("shop", { cat: c.name })}>
                <Ph label={c.label} ratio="portrait" />
                <div className="cat-card-cap">
                  <span className="mono">{c.n}</span>
                  <span className="serif">{c.name}</span>
                </div>
              </button>
            </Reveal>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ---------------- FEATURED PRODUCTS ---------------- */
function FeaturedRow({ title, eyebrow, products, onNav, link }) {
  const { addToCart, toggleWish, wishlist } = useContext(RBCtx);
  return (
    <section className="section-pad">
      <div className="wrap-wide">
        <Reveal className="section-head" style={{ marginBottom: 44 }}>
          <div>
            <Eyebrow>{eyebrow}</Eyebrow>
            <h2 className="serif" style={{ marginTop: 14 }}>{title}</h2>
          </div>
          <button className="link-arrow" onClick={() => onNav("shop", link || {})}>View all</button>
        </Reveal>
        <div className="product-grid">
          {products.map((p, i) => (
            <Reveal key={p.id} delay={(i % 4) + 1}>
              <ProductCard p={p} onNav={onNav} onAdd={addToCart} wished={wishlist.includes(p.id)} onWish={toggleWish} />
            </Reveal>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ---------------- STORY ---------------- */
function StorySection({ onNav }) {
  return (
    <section className="section-pad story-sec">
      <div className="wrap-wide story-grid">
        <Reveal className="story-media zoomable"><Ph label="LOOK · PURPLE UTILITY SET" ratio="portrait" /></Reveal>
        <div className="story-body">
          <Reveal><Eyebrow line>Our Story</Eyebrow></Reveal>
          <Reveal delay={1}><h2 className="serif story-h">Design first, colour loud.</h2></Reveal>
          <Reveal delay={2}>
            <p className="lede" style={{ marginBottom: 22 }}>
              {(window.BRAND?.story.lede) || "Chinasa Martins designs contemporary ready-to-wear out of Lagos."}
            </p>
            <p style={{ color: "var(--ink-soft)", marginBottom: 30 }}>
              {(window.BRAND?.story.body) || "Structured suiting, draped eveningwear, and denim and print separates built for people who dress with intent."}
            </p>
          </Reveal>
          <Reveal delay={3} className="row" style={{ gap: 18, flexWrap: "wrap" }}>
            <Btn onClick={() => onNav("about", {})}>Read our story</Btn>
          </Reveal>
        </div>
      </div>
    </section>
  );
}

/* ---------------- COLLECTIONS STRIP ---------------- */
function OccasionSection({ onNav }) {
  return (
    <section className="section-pad occ-sec">
      <div className="wrap-wide">
        <Reveal className="section-head center-head">
          <div className="center" style={{ width: "100%" }}>
            <Eyebrow>Curated edits</Eyebrow>
            <h2 className="serif" style={{ margin: "14px auto 0", maxWidth: "none" }}>Shop by collection</h2>
          </div>
        </Reveal>
        <div className="occ-grid">
          {RB.COLLECTIONS.map((c, i) => (
            <Reveal key={c.id} delay={(i % 4) + 1} className="occ-card zoomable">
              <button className="occ-card-btn" onClick={() => onNav("collections", {})}>
                <Ph label={c.label} ratio={i % 2 === 0 ? "tall" : "portrait"} />
                <div className="occ-overlay" />
                <div className="occ-cap">
                  <span className="mono">{c.tag}</span>
                  <span className="serif occ-title">{c.title}</span>
                  <span className="occ-blurb">{c.blurb}</span>
                  <span className="link-arrow on-img">Explore</span>
                </div>
              </button>
            </Reveal>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ---------------- LOOKBOOK TEASER ---------------- */
function LookbookTeaser({ onNav }) {
  return (
    <section className="lb-teaser">
      <Ph label="LOOK · RUFFLE CAPE GOWN" ratio="cinema" className="lb-teaser-bg" />
      <div className="lb-teaser-ov" />
      <div className="wrap lb-teaser-content">
        <Reveal><Eyebrow style={{ color: "var(--accent-bright)" }}>The Lookbook</Eyebrow></Reveal>
        <Reveal delay={1}><h2 className="serif lb-teaser-h">After Dark</h2></Reveal>
        <Reveal delay={2}><p className="lede on-img" style={{ maxWidth: "46ch", margin: "0 auto 28px" }}>
          Drape, colour and drama — the full shoot.
        </p></Reveal>
        <Reveal delay={3}><Btn onClick={() => onNav("lookbook", {})}>View the Lookbook</Btn></Reveal>
      </div>
    </section>
  );
}

/* ---------------- ENQUIRE CTA ---------------- */
function EnquireBand({ onNav }) {
  return (
    <section className="bespoke-band">
      <div className="wrap-wide bespoke-grid">
        <div className="bespoke-media zoomable"><Ph label="LOOK · LACE WRAP SET" ratio="square" /></div>
        <div className="bespoke-body">
          <Eyebrow line>Get in touch</Eyebrow>
          <h2 className="serif bespoke-h">See something you love?</h2>
          <p className="lede" style={{ marginBottom: 26 }}>
            Pricing and availability are shared on enquiry. DM {(window.BRAND && window.BRAND.igHandle) || "@nasamartins"} on Instagram
            or send a message and we'll get back to you directly.
          </p>
          <div className="hero-cta">
            <Btn onClick={() => onNav("contact", {})}>Send an enquiry</Btn>
            <Btn variant="ghost" arrow={false} href={(window.BRAND && window.BRAND.igUrl) || "#"}>Message on Instagram</Btn>
          </div>
        </div>
      </div>
    </section>
  );
}

/* ---------------- FAQ ---------------- */
function FAQ() {
  const [open, setOpen] = useState(0);
  return (
    <section className="section-pad">
      <div className="wrap faq-grid">
        <div className="faq-head">
          <Eyebrow line>Good to know</Eyebrow>
          <h2 className="serif" style={{ fontSize: "clamp(32px,4vw,52px)", fontWeight: 500, marginTop: 16, lineHeight: 1.04 }}>Frequently asked questions</h2>
          <p style={{ color: "var(--ink-soft)", marginTop: 18 }}>Can't find an answer? Message us on Instagram or use the contact form.</p>
        </div>
        <div className="faq-list">
          {RB.FAQS.map((f, i) => (
            <div className={"faq-item" + (open === i ? " open" : "")} key={i}>
              <button className="faq-q" onClick={() => setOpen(open === i ? -1 : i)}>
                <span>{f.q}</span><Icon name={open === i ? "minus" : "plus"} size={20} />
              </button>
              <div className="faq-a"><p>{f.a}</p></div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ---------------- INSTAGRAM STRIP ---------------- */
function CommunityStrip() {
  return (
    <section className="insta-sec">
      <div className="center" style={{ marginBottom: 32 }}>
        <Eyebrow>{(window.BRAND && window.BRAND.igHandle) || "@nasamartins"}</Eyebrow>
        <h2 className="serif" style={{ fontSize: "clamp(28px,3.4vw,42px)", fontWeight: 500, marginTop: 10 }}>From the shoot</h2>
      </div>
      <div className="insta-row">
        {[1, 2, 3, 4, 5, 6, 7, 8].map((i) => (
          <a key={i} className="insta-cell zoomable" href={(window.BRAND && window.BRAND.igUrl) || "#"} aria-label="Instagram">
            <Ph label={"@ · " + i} ratio="square" />
            <span className="insta-ic"><Icon name="ig" size={20} /></span>
          </a>
        ))}
      </div>
    </section>
  );
}

/* ---------------- PRODUCT CARD ---------------- */
function ProductCard({ p, onNav, onAdd, wished, onWish }) {
  return (
    <article className="pcard zoomable" onClick={() => onNav("product", { id: p.id })}>
      <div className="pcard-media">
        <button className={"wish" + (wished ? " active" : "")} onClick={(e) => { e.stopPropagation(); onWish(p.id); }} aria-label="Wishlist">
          <Icon name={wished ? "star-f" : "heart"} size={17} />
        </button>
        <Ph label={p.label} ratio="portrait" />
      </div>
      <div className="pcard-body">
        <span className="pcard-cat">{p.cat}</span>
        <span className="pcard-name">{p.name}</span>
        <span className="pcard-price"><Price ngn={p.price} /></span>
        <div className="swatches">{p.colors.slice(0, 4).map((c) => <span key={c.name} className="swatch" style={{ background: c.hex }} title={c.name} />)}</div>
      </div>
    </article>
  );
}

/* ---------------- HOME PAGE ---------------- */
function HomePage({ onNav, tweaks }) {
  const featured1 = RB.PRODUCTS.slice(0, 4);
  const featured2 = RB.PRODUCTS.slice(4, 8);
  return (
    <div className="fade-page">
      <Hero onNav={onNav} headline={tweaks.heroHeadline} sub={tweaks.heroSub} />
      <TrustBar />
      <CategoryStrip onNav={onNav} />
      <FeaturedRow eyebrow="The Edit" title="Shop the looks" products={featured1} onNav={onNav} />
      <FeaturedRow eyebrow="More looks" title="Also available" products={featured2} onNav={onNav} />
      <StorySection onNav={onNav} />
      <OccasionSection onNav={onNav} />
      <EnquireBand onNav={onNav} />
      <FAQ />
      <CommunityStrip />
    </div>
  );
}

Object.assign(window, { HomePage, ProductCard, Hero, FAQ });
