// Soulforce Arts — The Try-Harder Trap page app
const { useState: useStateA, useEffect: useEffectA } = React;

function TrapHeader({ onCTA, onOpenMenu, scrolled }) {
  const links = [
    { href: '/', label: 'Home' },
    { href: '/tensionreset', label: 'Free Lab' },
    { href: '/pwp', label: 'Perform Without Pain' },
    { href: '/book', label: 'The Book' },
    { href: '/about', label: 'About' },
  ];
  return (
    <header className={`header ${scrolled ? 'scrolled' : ''}`}>
      <a className="brand" href="/" aria-label="Soulforce Arts Institute — Home">
        <img src="assets/logo-gold-wide-light.png" alt="Soulforce Arts Institute" />
      </a>
      <div className="header-right">
        <nav aria-label="Primary">
          {links.map((l) => (
            <a key={l.label} href={l.href}>{l.label}</a>
          ))}
        </nav>
        <div className="cta-row">
          <button className="btn sm" onClick={() => onCTA('checkout')}>
            Get the class — $27
          </button>
          <button className="menu-toggle" onClick={onOpenMenu} aria-label="Menu">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5"><path d="M3 7h18M3 12h18M3 17h18" /></svg>
          </button>
        </div>
      </div>
    </header>
  );
}

function TrapDrawer({ open, onClose, onCTA }) {
  return (
    <div className={`mob-drawer ${open ? 'open' : ''}`}>
      <a href="/" onClick={onClose}>Home</a>
      <a href="/tensionreset" onClick={onClose}>Free Lab</a>
      <a href="/pwp" onClick={onClose}>Perform Without Pain</a>
      <a href="/book" onClick={onClose}>The Book</a>
      <a href="/about" onClick={onClose}>About</a>
      <div className="ctas">
        <button className="btn" onClick={() => { onClose(); onCTA('checkout'); }}>
          Get instant access — $27
        </button>
        <a className="btn outline" href="/tensionreset" onClick={onClose}>
          The free Lab
        </a>
      </div>
    </div>
  );
}

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "ensoIntensity": "subtle",
  "density": "regular"
}/*EDITMODE-END*/;

function TrapApp() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);
  const [modal, setModal] = useStateA(null);
  const [menuOpen, setMenuOpen] = useStateA(false);
  const [scrolled, setScrolled] = useStateA(false);

  useEffectA(() => {
    document.body.classList.toggle('enso-off', t.ensoIntensity === 'off');
    document.body.classList.toggle('enso-subtle', t.ensoIntensity === 'subtle');
    document.body.classList.toggle('enso-prominent', t.ensoIntensity === 'prominent');
    document.body.classList.remove('density-compact', 'density-comfy');
    if (t.density === 'compact') document.body.classList.add('density-compact');
    if (t.density === 'comfy') document.body.classList.add('density-comfy');
  }, [t.ensoIntensity, t.density]);

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

  const openCTA = (kind) => {
    if (kind === 'checkout') setModal('checkout');
    else if (kind === 'lab') window.location.href = '/tensionreset';
  };

  return (
    <>
      <TrapHeader onCTA={openCTA} onOpenMenu={() => setMenuOpen(true)} scrolled={scrolled} />
      <TrapDrawer open={menuOpen} onClose={() => setMenuOpen(false)} onCTA={openCTA} />

      <main id="top" className="page">
        <TrapHero onCTA={openCTA} />
        <TheTrap />
        <WhatYoullLearn />
        <QuoteA />
        <WhoForTrap />
        <AboutJoseph />
        <QuoteB />
        <PriceCTA onCTA={openCTA} />
        <TrapFaq />
      </main>

      <TrapFooter />

      {modal === 'checkout' && <CheckoutModal onClose={() => setModal(null)} />}

      <TweaksPanel title="Tweaks">
        <TweakSection label="Enso" />
        <TweakRadio label="Intensity" value={t.ensoIntensity}
          options={['off', 'subtle', 'prominent']}
          onChange={(v) => setTweak('ensoIntensity', v)} />
        <TweakSection label="Page rhythm" />
        <TweakRadio label="Whitespace" value={t.density}
          options={['compact', 'regular', 'comfy']}
          onChange={(v) => setTweak('density', v)} />
      </TweaksPanel>
    </>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<TrapApp />);
