/* global React, ReactDOM */
const { useEffect } = React;
const { useReveal, useMouseGlow, Marquee, Logos } = window.BMShared;
const { Nav, Hero, Problems, Pillars, Stats, Process, Pricing, FitCheck, Faq, Blog, CtaBanner, Footer } = window.BMSections;

const HERO_PRESETS = {
  default: {
    l1: "Ton business tourne.",
    l2: "Même sans toi.",
    sub: [
      "Pour les boîtes de 2 à 20 personnes.",
      "Sans recruter, sans agence trop chère, sans tech.",
      "Tu vois le résultat en moins de 90 jours."
    ]
  },
  bold: {
    l1: "Arrête de bosser",
    l2: "comme un stagiaire dans ta propre boîte.",
    sub: "On installe ton acquisition, tes workflows et tes agents IA en 4–6 semaines. ROI sous 90 jours, sinon on déploie pas."
  },
  direct: {
    l1: "+15h par semaine.",
    l2: "Rentable en 90 jours.",
    sub: "Une équipe tech au prix d'un stagiaire, qui livre en semaines. Cash but caring."
  },
  ai: {
    l1: "Tes outils existants,",
    l2: "augmentés par l'IA.",
    sub: "Claude, ChatGPT, Gemini intégrés là où ils ont un impact concret. Pas de spectacle, juste du ROI."
  }
};

const CTA_PRESETS = {
  audit: 'Audit gratuit',
  rdv: 'Réserver 30 min',
  boom: 'Faire BOOM',
  start: 'Démarrer maintenant'
};

const App = () => {
  const tw = window.useTweaks(/*EDITMODE-BEGIN*/{
    "theme": "light",
    "density": "comfy",
    "ctaStyle": "pill",
    "heroLayout": "split",
    "heroCopy": "default",
    "ctaText": "audit"
  }/*EDITMODE-END*/);

  /* Tweaks panel: only render in dev (?tweaks=1) so prod visitors never see it */
  const showTweaks = typeof window !== 'undefined' && window.location.search.includes('tweaks=1');

  const { theme, density, ctaStyle, heroLayout, heroCopy, ctaText } = tw.values;
  const setTweak = tw.setTweak;

  /* Propagate theme to <html> so body bg + cross-page state stay in sync */
  useEffect(() => {
    const root = document.documentElement;
    ['theme-violet', 'theme-cyber', 'theme-mono', 'theme-light'].forEach((c) => root.classList.remove(c));
    root.classList.add('theme-' + theme);
  }, [theme]);

  /* Re-scroll to anchor after React mount.
   * Sans ça, quand on arrive sur /index.html#process depuis /a-propos via
   * le dropdown "Expertises > Process", le navigateur tente de scroller
   * vers #process AVANT que React ait monté la section → l'élément
   * n'existe pas, le scroll échoue, l'utilisateur doit cliquer 2 fois. */
  useEffect(() => {
    if (window.location.hash) {
      const tryScroll = () => {
        const el = document.querySelector(window.location.hash);
        if (el) el.scrollIntoView({ behavior: 'instant', block: 'start' });
      };
      const t1 = setTimeout(tryScroll, 80);
      const t2 = setTimeout(tryScroll, 300);
      return () => { clearTimeout(t1); clearTimeout(t2); };
    }
  }, []);

  useReveal();
  useMouseGlow('.pillar');

  const heroData = HERO_PRESETS[heroCopy] || HERO_PRESETS.default;
  const ctaLabel = CTA_PRESETS[ctaText] || CTA_PRESETS.audit;
  const rootClass = `theme-${theme} density-${density} cta-style-${ctaStyle}`;

  return (
    <div className={rootClass} data-screen-label="01 Boom Maker Landing">
      <Nav />
      <Hero heroCopy={heroData} ctaText={ctaLabel} layout={heroLayout} />
      <Marquee items={[
        "ROI < 90 jours",
        "Sans coder · sans recruter",
        "4–6 semaines"
      ]} />
      <Logos />
      <Problems />
      <Pillars />
      <Stats />
      <Process />
      <Pricing ctaText={ctaLabel} />
      <FitCheck />
      {/* TODO: réactiver <Testimonials /> quand 3 vrais clients seront communicables (vrais noms + entreprise + photo + LinkedIn) */}
      <Faq />
      <CtaBanner ctaText={ctaLabel} />
      <Blog />
      <Footer />

      {showTweaks && <window.TweaksPanel title="Tweaks Boom Maker">
        <window.TweakSection title="Palette">
          <window.TweakRadio
            label="Thème"
            value={theme}
            onChange={(v) => setTweak('theme', v)}
            options={[
              { value: 'violet', label: 'Violet · IA (def.)' },
              { value: 'cyber', label: 'Cyber · pink/green' },
              { value: 'mono', label: 'Mono · noir' },
              { value: 'light', label: 'Clair' }
            ]}
          />
        </window.TweakSection>
        <window.TweakSection title="Hero">
          <window.TweakRadio
            label="Layout"
            value={heroLayout}
            onChange={(v) => setTweak('heroLayout', v)}
            options={[
              { value: 'split', label: 'Split + viz' },
              { value: 'stacked', label: 'Stacked' }
            ]}
          />
          <window.TweakSelect
            label="Texte du hero"
            value={heroCopy}
            onChange={(v) => setTweak('heroCopy', v)}
            options={[
              { value: 'default', label: 'Default · ON' },
              { value: 'bold', label: 'Bold · Stagiaire' },
              { value: 'direct', label: 'Direct · +15h' },
              { value: 'ai', label: 'AI · Outils augmentés' }
            ]}
          />
        </window.TweakSection>
        <window.TweakSection title="Call to action">
          <window.TweakRadio
            label="Style"
            value={ctaStyle}
            onChange={(v) => setTweak('ctaStyle', v)}
            options={[
              { value: 'pill', label: 'Pilule' },
              { value: 'square', label: 'Carré' },
              { value: 'block', label: 'Bloc' }
            ]}
          />
          <window.TweakSelect
            label="Texte du CTA"
            value={ctaText}
            onChange={(v) => setTweak('ctaText', v)}
            options={[
              { value: 'audit', label: 'Audit gratuit' },
              { value: 'rdv', label: 'Réserver 30 min' },
              { value: 'boom', label: 'Faire BOOM' },
              { value: 'start', label: 'Démarrer maintenant' }
            ]}
          />
        </window.TweakSection>
        <window.TweakSection title="Densité">
          <window.TweakRadio
            label="Espacement"
            value={density}
            onChange={(v) => setTweak('density', v)}
            options={[
              { value: 'cozy', label: 'Cozy' },
              { value: 'comfy', label: 'Comfy' },
              { value: 'spacious', label: 'Spacious' }
            ]}
          />
        </window.TweakSection>
      </window.TweaksPanel>}
    </div>
  );
};

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