/* global React, ReactDOM, Chrome, Hero, Problem, Vitor, Manifesto, How, Proof, Included, FAQ, Final, Footer, Marquee, Cities, Chat, ExitIntentModal, MobileStickyCta, UrgencyRibbon, MobileStats */

function App() {
  // WhatsApp link (with pre-filled tracking message)
  const WA = "https://api.whatsapp.com/send?phone=5547933838849&text=Oi%2C+vim+pela+landing+page+e+quero+minha+pr%C3%A9via";

  // ============================================================
  // Google Sheets integration (Apps Script Web App endpoint)
  // After deploying the Apps Script (instructions in setup/apps-script.gs),
  // paste the resulting /exec URL here.
  // ============================================================
  const SHEETS_WEBAPP_URL = "https://script.google.com/macros/s/COLE_AQUI_O_ID_DO_SEU_DEPLOY/exec";

  React.useEffect(() => {
    window.__sendToSheet = async (data) => {
      if (!SHEETS_WEBAPP_URL || SHEETS_WEBAPP_URL.includes("COLE_AQUI")) return;
      try {
        // text/plain avoids CORS preflight — Apps Script accepts JSON in body
        await fetch(SHEETS_WEBAPP_URL, {
          method: "POST",
          mode: "no-cors",
          headers: { "Content-Type": "text/plain;charset=utf-8" },
          body: JSON.stringify({
            ...data,
            timestamp: new Date().toISOString(),
            userAgent: navigator.userAgent,
            referrer: document.referrer || "(direto)",
          }),
        });
      } catch (err) { /* swallow */ }
    };
  }, []);

  // Pixel + tracking helpers
  React.useEffect(() => {
    window.__pixel = (event, params) => {
      try {
        if (typeof window.fbq === "function") {
          window.fbq("track", event, params || {});
        }
      } catch (e) { /* noop */ }
    };
    window.__trackWhatsApp = (origin) => {
      window.__pixel?.("Lead", { content_name: "ClickWhatsApp", origin, value: 197, currency: "BRL" });
    };
    // PageView already fires in the Pixel script tag in <head>
  }, []);

  return (
    <>
      <UrgencyRibbon />
      <Chrome wa={WA} />
      <main>
        <Hero wa={WA} />
        <Marquee />
        <Problem />
        <Vitor />
        <Manifesto />
        <How />
        <Chat />
        <Proof />
        <Included />
        <Cities />
        <FAQ />
        <Final wa={WA} />
      </main>
      <Footer wa={WA} />
      <ExitIntentModal wa={WA} />
      <MobileStickyCta wa={WA} />
    </>
  );
}

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