// home-hero.jsx — Four hero variants for the full-viewport homepage.
// All four share the same nav and corner-meta; they differ in composition.

const { useEffect: heroUseEffect, useState: heroUseState } = React;

function SiteNav({ mood: propMood = "dusk", hero = "a", onToggleTweaks }) {
  const [scrolled, setScrolled] = heroUseState(false);
  const [keypadOpen, setKeypadOpen] = heroUseState(false);
  const [mobileMenuOpen, setMobileMenuOpen] = heroUseState(false);

  // Sync state globally via useTweaks hook
  const [t, setTweak] = useTweaks({ mood: propMood });
  const activeMood = t.mood;

  heroUseEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 24);
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  heroUseEffect(() => {
    if (mobileMenuOpen) {
      document.body.style.overflow = 'hidden';
    } else {
      document.body.style.overflow = '';
    }
    return () => { document.body.style.overflow = ''; };
  }, [mobileMenuOpen]);

  heroUseEffect(() => {
    if (!mobileMenuOpen) return;
    const onKey = (e) => { if (e.key === 'Escape') setMobileMenuOpen(false); };
    window.addEventListener('keydown', onKey);
    return () => window.removeEventListener('keydown', onKey);
  }, [mobileMenuOpen]);

  const isHome = typeof window !== 'undefined' && (
    window.location.pathname.endsWith("Home.html") ||
    window.location.pathname.endsWith("/") ||
    window.location.pathname === "" ||
    !window.location.pathname.includes(".")
  );

  const getLink = (hash) => {
    return isHome ? hash : `Home.html${hash}`;
  };

  const isActive = (path) => {
    if (typeof window === 'undefined') return false;
    const current = window.location.pathname.toLowerCase();
    const target = path.toLowerCase();
    return current.endsWith(target) || current.includes("/" + target);
  };

  const darkNav = scrolled 
    ? (activeMood === "night") 
    : (isHome 
        ? ((hero === "a" || hero === "c") && (activeMood === "dusk" || activeMood === "night"))
        : (activeMood === "night")
      );

  const c = mobileMenuOpen ? "#ece4d4" : (darkNav ? "#ece4d4" : "var(--ink)");

  const scenes = [
    { id: "day",    label: "Morning", preset: "morning" },
    { id: "golden", label: "Dinner",  preset: "dinner" },
    { id: "dusk",   label: "Evening", preset: "evening" },
    { id: "night",  label: "Night",   preset: "night" }
  ];

  return (
    <React.Fragment>
    <header className={"site-nav " + (scrolled ? "is-scrolled" : "") + (mobileMenuOpen ? " mobile-nav-open" : "")} data-on-dark={darkNav ? "true" : "false"}>
      <div className="site-nav-header-row">
        <a href={isHome ? "#" : "Home.html"} className="site-nav-logo" style={{color: c}}>
          <HLMark size={32} inkColor={c} />
          <HLWordmark size={11} color={c} tracking="0.34em" />
        </a>
        
        <nav className="site-nav-links" style={{color: c, position: 'relative'}}>
          <a href={getLink("#studio")}>Studio</a>
          <a href="Services.html" className={isActive("Services.html") ? "active" : ""}>Services</a>
          <a href="Portfolio.html" className={isActive("Portfolio.html") ? "active" : ""}>Portfolio</a>
          <a href={getLink("#process")}>Process</a>
          <a href={getLink("#journal")}>Journal</a>
          <a href={getLink("#contact")}>Contact</a>
          <button
            type="button"
            className="site-nav-atmosphere-btn"
            onClick={() => setKeypadOpen(!keypadOpen)}
            style={{
              background: 'transparent',
              border: 'none',
              color: 'inherit',
              fontFamily: 'inherit',
              fontSize: 'inherit',
              letterSpacing: 'inherit',
              textTransform: 'inherit',
              fontWeight: 'inherit',
              cursor: 'pointer',
              padding: '4px 0',
              display: 'flex',
              alignItems: 'center',
              gap: '8px',
              opacity: 0.75,
              transition: 'opacity 0.3s ease'
            }}
            onMouseEnter={(e) => e.target.style.opacity = 1}
            onMouseLeave={(e) => e.target.style.opacity = 0.75}
          >
            <span className="keypad-led" style={{
              display: 'inline-block',
              width: 6,
              height: 6,
              borderRadius: '50%',
              border: '1px solid rgba(176, 138, 98, 0.4)',
              background: activeMood === 'day' ? '#f5eedc' : activeMood === 'golden' ? '#e2b474' : activeMood === 'dusk' ? '#c48d68' : '#946747',
              boxShadow: activeMood === 'day' 
                ? '0 0 6px rgba(245, 238, 220, 0.8)' 
                : activeMood === 'golden' 
                  ? '0 0 6px rgba(226, 180, 116, 0.8)' 
                  : activeMood === 'dusk' 
                    ? '0 0 6px rgba(196, 141, 104, 0.8)' 
                    : '0 0 6px rgba(148, 103, 71, 0.5)',
              transition: 'all 0.35s cubic-bezier(0.22, 1, 0.36, 1)'
            }} />
            Atmosphere
          </button>

          {keypadOpen && (
            <>
              <div 
                className="nav-keypad-popup-backdrop" 
                onClick={() => setKeypadOpen(false)}
                style={{
                  position: 'fixed',
                  inset: 0,
                  zIndex: 9999,
                  background: 'transparent',
                  cursor: 'default'
                }}
              />
              <div 
                className="nav-keypad-popup"
                style={{
                  position: 'absolute',
                  top: '100%',
                  right: 0,
                  marginTop: '12px',
                  zIndex: 10000,
                  background: 'rgba(26, 23, 20, 0.95)',
                  border: '1px solid rgba(176, 138, 98, 0.3)',
                  borderRadius: '6px',
                  padding: '16px',
                  width: '180px',
                  boxShadow: '0 12px 32px rgba(0,0,0,0.5)',
                  backdropFilter: 'blur(12px)',
                  WebkitBackdropFilter: 'blur(12px)',
                  display: 'flex',
                  flexDirection: 'column',
                  gap: '12px'
                }}
              >
                <div style={{ 
                  display: 'flex', 
                  justifyContent: 'space-between', 
                  alignItems: 'center',
                  borderBottom: '1px solid rgba(255,255,255,0.06)',
                  paddingBottom: '8px',
                  marginBottom: '4px'
                }}>
                  <span style={{ 
                    fontFamily: 'var(--f-mono)', 
                    fontSize: '9px', 
                    letterSpacing: '0.14em', 
                    color: 'var(--brass)',
                    textTransform: 'uppercase'
                  }}>
                    Lutron Alisse
                  </span>
                  <button 
                    onClick={() => setKeypadOpen(false)}
                    style={{
                      background: 'transparent',
                      border: 'none',
                      color: 'rgba(255,255,255,0.4)',
                      fontSize: '11px',
                      cursor: 'pointer',
                      padding: 0
                    }}
                    onMouseEnter={(e) => e.target.style.color = '#fff'}
                    onMouseLeave={(e) => e.target.style.color = 'rgba(255,255,255,0.4)'}
                  >
                    ✕
                  </button>
                </div>
                <div className="keypad-plate" style={{ 
                  padding: '12px 10px', 
                  width: '100%',
                  background: 'linear-gradient(135deg, #2b1f16 0%, #17110c 100%)',
                  border: '1px solid rgba(0,0,0,0.4)',
                  boxShadow: 'inset 0 1px 2px rgba(255,255,255,0.05), 0 4px 12px rgba(0,0,0,0.3)',
                  borderRadius: '4px'
                }}>
                  <div style={{ display: 'flex', flexDirection: 'column', gap: '8px' }}>
                    {scenes.map((s) => (
                      <button
                        key={s.id}
                        type="button"
                        className="keypad-button"
                        data-active={activeMood === s.id ? "true" : "false"}
                        onClick={() => {
                          setTweak('mood', s.id);
                          window.dispatchEvent(new CustomEvent('luminaScenePreset', {
                            detail: { preset: s.preset, mood: s.id }
                          }));
                          setKeypadOpen(false);
                        }}
                        style={{
                          padding: '10px 8px',
                          display: 'flex',
                          justifyContent: 'space-between',
                          alignItems: 'center',
                          width: '100%',
                          boxSizing: 'border-box'
                        }}
                      >
                        <span className="keypad-button-label" style={{ fontSize: '7.5px' }}>{s.label}</span>
                        <span className="keypad-led" />
                      </button>
                    ))}
                  </div>
                  <span className="keypad-screw-bottom" />
                </div>
              </div>
            </>
          )}
        </nav>

        {/* Hamburger Menu Toggle Button (Visible on Mobile) */}
        <button
          type="button"
          className={"mobile-nav-toggle " + (mobileMenuOpen ? "is-open" : "")}
          onClick={() => setMobileMenuOpen(!mobileMenuOpen)}
          aria-label="Toggle navigation menu"
          aria-expanded={mobileMenuOpen}
          aria-controls="mobile-nav-panel"
          style={{ color: c }}
        >
          <span className="hamburger-line line-1"></span>
          <span className="hamburger-line line-2"></span>
          <span className="hamburger-line line-3"></span>
        </button>
      </div>
    </header>

    {/* Mobile Menu Backdrop + Panel — rendered as SIBLING of the header (not
        a child) so the nav's backdrop-filter doesn't trap their fixed
        positioning inside the nav's containing block on iOS Safari. */}
    {mobileMenuOpen && (
      <div
        className="mobile-nav-backdrop"
        onClick={() => setMobileMenuOpen(false)}
        aria-hidden="true"
      />
    )}
    {mobileMenuOpen && (
      <div id="mobile-nav-panel" className="mobile-nav-dropdown" role="dialog" aria-modal="true">
          <nav className="mobile-menu-links">
            <a href={getLink("#studio")} onClick={() => setMobileMenuOpen(false)}>Studio</a>
            <a href="Services.html" className={isActive("Services.html") ? "active" : ""} onClick={() => setMobileMenuOpen(false)}>Services</a>
            <a href="Portfolio.html" className={isActive("Portfolio.html") ? "active" : ""} onClick={() => setMobileMenuOpen(false)}>Portfolio</a>
            <a href={getLink("#process")} onClick={() => setMobileMenuOpen(false)}>Process</a>
            <a href={getLink("#journal")} onClick={() => setMobileMenuOpen(false)}>Journal</a>
            <a href={getLink("#contact")} onClick={() => setMobileMenuOpen(false)}>Contact</a>
          </nav>

          <div className="mobile-menu-atmosphere">
            <div className="mobile-atmosphere-title">
              <span>LUTRON ALISSE KEYPAD</span>
              <span className="keypad-sub">Select Atmosphere</span>
            </div>
            
            <div className="keypad-plate mobile-keypad">
              <div className="mobile-keypad-grid">
                {scenes.map((s) => (
                  <button
                    key={s.id}
                    type="button"
                    className="keypad-button"
                    data-active={activeMood === s.id ? "true" : "false"}
                    onClick={() => {
                      setTweak('mood', s.id);
                      window.dispatchEvent(new CustomEvent('luminaScenePreset', {
                        detail: { preset: s.preset, mood: s.id }
                      }));
                      setMobileMenuOpen(false);
                    }}
                    style={{
                      display: 'flex',
                      justifyContent: 'space-between',
                      alignItems: 'center',
                      boxSizing: 'border-box'
                    }}
                  >
                    <span className="keypad-button-label" style={{ fontSize: '8px' }}>{s.label}</span>
                    <span className="keypad-led" style={{
                      display: 'inline-block',
                      width: 5,
                      height: 5,
                      borderRadius: '50%',
                      background: activeMood === s.id 
                        ? (s.id === 'day' ? '#f5eedc' : s.id === 'golden' ? '#e2b474' : s.id === 'dusk' ? '#c48d68' : '#946747') 
                        : 'rgba(255,255,255,0.06)',
                      boxShadow: activeMood === s.id 
                        ? `0 0 6px ${s.id === 'day' ? '#f5eedc' : s.id === 'golden' ? '#e2b474' : s.id === 'dusk' ? '#c48d68' : '#946747'}` 
                        : 'none',
                      border: '1px solid rgba(0,0,0,0.25)',
                      transition: 'all 0.3s ease'
                    }} />
                  </button>
                ))}
              </div>
              <span className="keypad-screw-bottom" />
            </div>
          </div>

        <div className="mobile-menu-footer">
          MAUI · HAWAI‘I &nbsp;·&nbsp; EST. 2019
        </div>
      </div>
    )}
    </React.Fragment>
  );
}

// ── Hero A — Full-bleed dusk ──────────────────────────────────────────
function FullHeroA() {
  return (
    <section className="hv hv-a" data-screen-label="01 Hero A · Dusk">
      <div className="hv-a-bg">
        <image-slot id="home-heroA" placeholder="Dusk interior · ocean horizon · warm fixtures" shape="rect"></image-slot>
      </div>
      <div className="hv-a-vignette" />
      <div className="hv-a-content container" data-reveal>
        <span className="label" data-comment-anchor="hero-eyebrow">Interior &amp; Light Direction · Private Residences</span>
        <h1 className="hv-a-h1">
          A home should not lose its beauty
          <em> after sunset.</em>
        </h1>
        <p className="hv-a-sub">
          Hale Lumina unifies interior design and architectural lighting to compose private residences in material, shadow, warmth, and light — calm by day, alive by night.
        </p>
        <div className="hv-a-ctas">
          <a className="btn btn-primary" href="Portfolio.html">Explore Our Work <span className="arrow">→</span></a>
          <a className="btn btn-ghost-light" href="#contact">Begin a Consultation</a>
        </div>
      </div>
      <div className="hv-a-meta-l">MAUI · HAWAI‘I &nbsp;·&nbsp; EST. 2019</div>
      <div className="hv-a-meta-r">01 / HERO — DUSK COMPOSITION</div>
    </section>
  );
}

// ── Hero B — Editorial split ──────────────────────────────────────────
function FullHeroB() {
  return (
    <section className="hv hv-b" data-screen-label="01 Hero B · Split">
      <div className="hv-b-text" data-reveal>
        <div>
          <span className="label">01 — Atelier · Maui</span>
          <h1 className="hv-b-h1">
            Homes composed in <em>material,</em> shadow, and light.
          </h1>
          <p className="lead">
            An interior and lighting design collaboration for private residences. We compose the visible and invisible elements of home into one continuous atmosphere — from morning into night.
          </p>
          <div className="hv-b-ctas">
            <a className="btn btn-primary" href="Portfolio.html">Explore Our Work <span className="arrow">→</span></a>
            <a className="btn btn-ghost" href="#contact">Begin a Consultation</a>
          </div>
        </div>
        <div className="hv-b-meta">
          <span>HALE LUMINA · INTERIOR &amp; LIGHT DIRECTION</span>
          <span>VOL. 01</span>
        </div>
      </div>
      <div className="hv-b-image">
        <image-slot id="home-heroB" placeholder="Plaster wall · grazing brass light" shape="rect"></image-slot>
      </div>
    </section>
  );
}

// ── Hero C — Dark monograph ───────────────────────────────────────────
function FullHeroC() {
  return (
    <section className="hv hv-c section-dark" data-screen-label="01 Hero C · Monograph">
      <div className="hv-c-grid">
        <div className="hv-c-top">
          <span className="label" style={{color:'var(--brass)'}}>The Lumina Method · Vol. I</span>
          <span className="hv-c-year">2019 — 2026</span>
        </div>
        <h1 className="hv-c-h1" data-reveal>
          Light, <span className="brass">composed.</span>
        </h1>
        <div className="hv-c-bottom">
          <div>
            <span className="label" style={{color:'var(--brass)'}}>A Quiet Practice</span>
            <p>Hale Lumina is a residential atmosphere atelier — we design the visible and invisible parts of home together.</p>
          </div>
          <div>
            <span className="label" style={{color:'var(--brass)'}}>Now Composing</span>
            <p>Three residences along the Hāmākua coast and a kitchen retreat in Hanalei.</p>
          </div>
          <div className="hv-c-ctas">
            <a className="btn btn-primary" href="#contact">Begin a Consultation <span className="arrow">→</span></a>
            <a className="tlink" href="Portfolio.html" style={{color:'var(--brass)'}}>View Selected Work <span className="arrow">→</span></a>
          </div>
        </div>
      </div>
    </section>
  );
}

// ── Hero D — Asymmetric atelier ───────────────────────────────────────
function FullHeroD() {
  return (
    <section className="hv hv-d" data-screen-label="01 Hero D · Atelier">
      <hr className="hv-d-rule" />
      <div className="hv-d-grid">
        <h1 className="hv-d-h1" data-reveal>
          The home, <em>fully seen.</em><br/>
          The home, <em>fully felt.</em>
        </h1>
        <div className="hv-d-body" data-reveal>
          <span className="label">Two Disciplines · One Vision</span>
          <p>
            Interiors and lighting are most often decided by different people at different stages.
            We compose them together — material, proportion, shadow, warmth, and light — so the residence feels complete from morning through night.
          </p>
          <div className="hv-d-ctas">
            <a className="btn btn-primary" href="Portfolio.html">Explore Our Work <span className="arrow">→</span></a>
            <a className="btn btn-ghost" href="#contact">Begin a Consultation</a>
          </div>
          <div className="hv-d-tags">
            <span>EST. 2019</span><span>·</span><span>MAUI · HAWAI‘I</span><span>·</span><span>BY APPOINTMENT</span>
          </div>
        </div>
        <div className="hv-d-image">
          <image-slot id="home-heroD" placeholder="Aged brass sconce · plaster · shadow" shape="rect"></image-slot>
        </div>
      </div>
    </section>
  );
}

function Hero({ variant = "a" }) {
  const v = (variant || "a").toLowerCase();
  if (v === "b") return <FullHeroB />;
  if (v === "c") return <FullHeroC />;
  if (v === "d") return <FullHeroD />;
  return <FullHeroA />;
}

Object.assign(window, { SiteNav, Hero, FullHeroA, FullHeroB, FullHeroC, FullHeroD });
