/* global React, ReactDOM, useTweaks, TweaksPanel, TweakSection, TweakRadio, TweakColor, TweakSelect */
const { useState, useEffect, useRef } = React;

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "surface": "paper",
  "accent": "#ff4800",
  "hero": "pipeline"
}/*EDITMODE-END*/;

function App() {
  const [tweaks, setTweak] = useTweaks(TWEAK_DEFAULTS);
  const [bookingOpen, setBookingOpen] = useState(false);

  // Apply surface + accent to <body>
  useEffect(() => {
    document.body.dataset.surface = tweaks.surface;
    document.body.dataset.accent = "1";
    document.body.style.setProperty("--accent-override", tweaks.accent);
  }, [tweaks.surface, tweaks.accent]);

  // Scroll reveals
  useEffect(() => {
    const els = document.querySelectorAll('.reveal');
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) {
          e.target.classList.add('is-visible');
          io.unobserve(e.target);
        }
      });
    }, { threshold: 0.12 });
    els.forEach(el => io.observe(el));
    return () => io.disconnect();
  }, []);

  const openBooking = () => setBookingOpen(true);

  return (
    <>
      <Header onBook={openBooking} />
      <main id="top">
        {/* HERO */}
        <section className={`hero ${tweaks.hero === 'editorial' ? 'hero--editorial' : ''}`}>
          <div className="container">
            {tweaks.hero === 'editorial' ? (
              <div style={{textAlign:'center', maxWidth:'56rem', margin:'0 auto'}}>
                <div className="hero-eyebrow" style={{justifyContent:'center'}}>
                  <span className="swatch" />
                  <span className="eyebrow">Sales agent · scoped, not self-serve</span>
                </div>
                <h1 className="h-mega">
                  The sales agent you hire <em>before your next rep.</em>
                </h1>
                <p className="hero-sub" style={{margin:'2rem auto 0'}}>
                  Portant.ai runs every deal from meeting booked to signed contract. Drafts the follow-ups. Builds the proposals, quotes, and contracts. Schedules the calls your reps need to make. Closes the loop.
                </p>
                <div className="hero-cta" style={{justifyContent:'center'}}>
                  <button className="btn btn--primary btn--lg" onClick={openBooking}>Book a scoping call →</button>
                  <a className="btn btn--ghost btn--lg" href="#how-it-works">See how it works</a>
                </div>
                <div style={{marginTop:'4rem'}}>
                  <EditorialDecor />
                </div>
              </div>
            ) : (
              <div className="hero-grid">
                <div>
                  <div className="hero-eyebrow">
                    <span className="swatch" />
                    <span className="eyebrow">Sales agent · scoped, not self-serve</span>
                  </div>
                  <h1 className="h-mega">
                    The sales agent you hire <em>before your next rep.</em>
                  </h1>
                  <p className="hero-sub">
                    Portant.ai runs every deal from meeting booked to signed contract. Drafts the follow-ups. Builds the proposals, quotes, and contracts. Schedules the calls your reps need to make. Closes the loop.
                  </p>
                  <div className="hero-cta">
                    <button className="btn btn--primary btn--lg" onClick={openBooking}>Book a scoping call →</button>
                    <a className="btn btn--ghost btn--lg" href="#how-it-works">See how it works</a>
                  </div>
                  <div className="hero-trust">
                    <span>Built by the Portant team</span>
                    <span className="sep" />
                    <span>HubSpot Leading Tier Partner</span>
                    <span className="sep" />
                    <span>4.9 on G2 · 3,000+ installs</span>
                  </div>
                </div>
                <div>
                  {tweaks.hero === 'pipeline' && <PipelinePanel />}
                  {tweaks.hero === 'product' && <ProductSplit />}
                  {tweaks.hero === 'layered' && <LayeredArtifacts />}
                </div>
              </div>
            )}
          </div>
        </section>

        {/* Trust strip below hero in non-editorial */}
        {tweaks.hero === 'editorial' && (
          <div style={{borderTop:'1px solid var(--hairline)', borderBottom:'1px solid var(--hairline)'}}>
            <TrustStrip />
          </div>
        )}

        <HiringProblem />
        <MultiplyReps />
        <EndToEndFlow />
        <Comparison />
        <NotAnSDR />
        <DocumentMoat />
        <WhatWeBuild />
        <Pricing onBook={openBooking} />
        <WhoFor />
        <AboutPortant />
        <ClosingCTA onBook={openBooking} />
      </main>
      <Footer />

      <BookingModal open={bookingOpen} onClose={() => setBookingOpen(false)} />

      <TweaksPanel title="Tweaks">
        <TweakSection title="Surface mode">
          <TweakRadio value={tweaks.surface} onChange={(v) => setTweak('surface', v)}
            options={[
              {value: 'ink', label: 'Ink'},
              {value: 'paper', label: 'Paper'},
              {value: 'bone', label: 'Bone'},
            ]} />
        </TweakSection>
        <TweakSection title="Hero treatment">
          <TweakRadio value={tweaks.hero} onChange={(v) => setTweak('hero', v)}
            options={[
              {value: 'pipeline', label: 'Pipeline'},
              {value: 'product', label: 'Product'},
              {value: 'layered', label: 'Layered'},
              {value: 'editorial', label: 'Editorial'},
            ]} />
        </TweakSection>
        <TweakSection title="Accent color">
          <TweakColor value={tweaks.accent} onChange={(v) => setTweak('accent', v)} />
          <div style={{display:'flex', gap:'0.4rem', marginTop:'0.5rem', flexWrap:'wrap'}}>
            {['#ff4800', '#0044f1', '#1b6b4a', '#e63312', '#ffaa00'].map(c => (
              <button key={c} onClick={() => setTweak('accent', c)} style={{
                width:'24px', height:'24px', borderRadius:'6px', background:c,
                border: tweaks.accent === c ? '2px solid #fff' : '1px solid rgba(255,255,255,0.2)',
                cursor:'pointer', padding:0
              }} />
            ))}
          </div>
        </TweakSection>
      </TweaksPanel>
    </>
  );
}

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