// VeloFy LP — App orchestrator + Tweaks panel (shared state via tweakchange event)
const { useState: useS, useEffect: useE } = React;

const T = window.VFTop;
const B = window.VFBot;

// Sticky CTA shown after scrolling past hero
function StickyCta({ cta }) {
  const [show, setShow] = useS(false);
  useE(() => {
    const onScroll = () => {
      const y = window.scrollY;
      const max = document.body.scrollHeight - window.innerHeight - 400;
      setShow(y > 800 && y < max);
    };
    window.addEventListener('scroll', onScroll, { passive: true });
    onScroll();
    return () => window.removeEventListener('scroll', onScroll);
  }, []);
  const ctaCfg = T.CTA_VARIANTS[cta];
  return (
    <div className={'sticky-cta' + (show ? ' show' : '')}>
      <div className="label"><strong>R$ 0 pra sempre.</strong> Sem cartão.</div>
      <a href="/cadastro" className="btn btn-primary">🚀 {ctaCfg.label}</a>
    </div>
  );
}

// ─── App + Tweaks (single tree, shared state) ─────────────
function Root() {
  const defaults = window.__TWEAK_DEFAULTS__ || { headline: 'A', cta: 'A', palette: 'default', showCalculator: true };
  const [tw, setTweak] = window.useTweaks(defaults);

  // Apply palette to <html>
  useE(() => {
    document.documentElement.setAttribute('data-palette', tw.palette || 'default');
  }, [tw.palette]);

  return (
    <>
      <T.Nav />
      <T.Hero headline={tw.headline} cta={tw.cta} />
      <T.Marquee />
      <T.ValueProp />
      <T.Pain />
      <T.Solution />
      <B.Benefits />
      {tw.showCalculator && <B.Calculator />}
      <B.Social />
      <B.Offer />
      <B.Objections />
      <B.Guarantee />
      <B.Urgency />
      <B.FAQ />
      <B.Final cta={tw.cta} />
      <B.Footer />
      <StickyCta cta={tw.cta} />

      <window.TweaksPanel title="VeloFy · Tweaks">
        <window.TweakSection title="Conteúdo">
          <window.TweakRadio
            label="Headline"
            value={tw.headline}
            onChange={(v) => setTweak('headline', v)}
            options={[
              { value: 'A', label: 'Anti-Shopify' },
              { value: 'B', label: 'Mecanismo' },
              { value: 'C', label: 'Oferta' },
            ]}
          />
          <window.TweakRadio
            label="CTA"
            value={tw.cta}
            onChange={(v) => setTweak('cta', v)}
            options={[
              { value: 'A', label: 'Criar loja' },
              { value: 'B', label: 'Parar de perder' },
            ]}
          />
        </window.TweakSection>
        <window.TweakSection title="Visual">
          <TweakSelect
            label="Paleta"
            value={tw.palette}
            onChange={(v) => setTweak('palette', v)}
            options={[
              { value: 'default', label: 'Azul + Mint (brand)' },
              { value: 'midnight', label: 'Midnight blue' },
              { value: 'aqua', label: 'Aqua / mint forward' },
              { value: 'light', label: 'Light mode' },
            ]}
          />
        </window.TweakSection>
        <window.TweakSection title="Seções">
          <window.TweakToggle
            label="Mostrar calculadora"
            value={!!tw.showCalculator}
            onChange={(v) => setTweak('showCalculator', v)}
          />
        </window.TweakSection>
      </window.TweaksPanel>
    </>
  );
}

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