// splash-app.jsx — root App, ties sections, Tweaks, toast, and signup focus.

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "hero": "dark",
  "showWaveform": true,
  "accentDensity": "balanced"
}/*EDITMODE-END*/;

function App() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);
  const [toast, setToast] = React.useState(null);
  const signupApi = React.useRef(null);

  const dark = t.hero === 'dark';

  // Reflect hero theme on <body> so above-the-fold scroll doesn't reveal navy
  // under a bone hero (or vice versa).
  React.useEffect(() => {
    document.body.dataset.hero = t.hero;
  }, [t.hero]);

  const handleJoin = () => {
    const el = document.getElementById('signup');
    if (el) {
      el.scrollIntoView({ behavior: 'smooth', block: 'start' });
      setTimeout(() => signupApi.current && signupApi.current.focus(), 600);
    }
  };

  const handleSignup = (email) => {
    // POST silently to the Google Form. Browsers can't read the response
    // (Google Forms doesn't send CORS headers), so we use mode: 'no-cors'
    // and treat the submission as success — entries still land in the
    // form's responses tab.
    const FORM_ID = '1FAIpQLSfz-_Nj7A4HW-187cH1pAvOdEOV2xrsvLU_KItn9Ccu_A4dSg';
    const EMAIL_ENTRY = 'entry.95437541';
    const url = `https://docs.google.com/forms/d/e/${FORM_ID}/formResponse`;

    const data = new FormData();
    data.append(EMAIL_ENTRY, email);

    fetch(url, { method: 'POST', mode: 'no-cors', body: data })
      .catch(() => { /* opaque — ignore */ });

    setToast({
      title: "You're on the list.",
      body: `We'll email ${email} when BAUX goes live.`,
    });
    setTimeout(() => setToast(null), 5200);
  };

  const handlePartnerSubmit = (info) => {
    // POST silently to the Partners Google Form. See handleSignup for notes
    // on why mode: 'no-cors' is used and what the trade-offs are.
    const FORM_ID = '1FAIpQLScTMfprdIubKq5r-uRbM8WuKEFlLy5Egi5ZLsLPP-87t8yeNA';
    const ENTRIES = {
      name:  'entry.1245708855',
      org:   'entry.1132719911',
      email: 'entry.1050417673',
      notes: 'entry.32820924',
    };
    const url = `https://docs.google.com/forms/d/e/${FORM_ID}/formResponse`;

    const data = new FormData();
    data.append(ENTRIES.name,  info.name  || '');
    data.append(ENTRIES.org,   info.org   || '');
    data.append(ENTRIES.email, info.email || '');
    data.append(ENTRIES.notes, info.notes || '');

    fetch(url, { method: 'POST', mode: 'no-cors', body: data })
      .catch(() => { /* opaque — ignore */ });

    setToast({
      title: "Thanks — we'll be in touch.",
      body: `Talk soon, ${info.name.split(' ')[0] || info.org}.`,
    });
    setTimeout(() => setToast(null), 5200);
  };

  return (
    <div data-screen-label="BAUX BETA splash">
      <Nav dark={dark} onJoinClick={handleJoin}/>
      <Hero dark={dark} onJoinClick={handleJoin} onSubmitEmail={handleSignup}/>
      <Partners onSubmit={handlePartnerSubmit}/>
      <Signup signupRef={signupApi} onSubmit={handleSignup}/>
      <Footer/>

      {toast && (
        <div className="toast" role="status" aria-live="polite">
          <span className="dot"/>
          <span>
            <strong style={{ fontWeight: 600 }}>{toast.title}</strong>
            <span style={{ color: baux.white72, marginLeft: 6 }}>{toast.body}</span>
          </span>
          <button
            aria-label="Dismiss"
            onClick={() => setToast(null)}
            style={{
              appearance: 'none', border: 'none', background: 'transparent',
              color: baux.white72, cursor: 'pointer',
              padding: 4, marginLeft: 8,
              display: 'inline-flex',
            }}>
            <svg width="14" height="14" viewBox="0 0 14 14" aria-hidden>
              <path d="M3 3 L11 11 M11 3 L3 11" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/>
            </svg>
          </button>
        </div>
      )}

      <TweaksPanel title="Tweaks">
        <TweakSection label="Hero treatment"/>
        <TweakRadio
          label="Theme"
          value={t.hero}
          options={['dark', 'light']}
          onChange={(v) => setTweak('hero', v)}
        />
      </TweaksPanel>
    </div>
  );
}

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