/* global React, useReveal, Btn, CornerTicks, Ph, SignalLine, STATIONS */
const { useState: useStateH, useEffect: useEffectH } = React;

function Eyebrow({ children }) {
  return (
    <span className="eyebrow"><span className="bar" />{children}</span>
  );
}

/* ---------- Hero visual proof (labeled placeholder — swap in real screenshot) ---------- */
function ProductProof() {
  return (
    <div className="card" style={{ position: 'relative', overflow: 'hidden', textAlign: 'left', background: 'var(--bg-2)' }}>
      <CornerTicks color="var(--accent)" />
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between',
           padding: '12px 16px', borderBottom: '1px solid var(--line)', gap: 12, flexWrap: 'wrap' }}>
        <span className="mono" style={{ display: 'inline-flex', alignItems: 'center', gap: 9,
              fontSize: '0.68rem', letterSpacing: '0.1em', color: 'var(--ink-2)' }}>
          XAINT — DIAGNOSIS VIEW
        </span>
        <span className="mono" style={{ fontSize: '0.6rem', color: 'var(--ink-3)', letterSpacing: '0.06em' }}>ILLUSTRATIVE PREVIEW</span>
      </div>
      <div style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '14px 16px',
           borderBottom: '1px solid var(--line)', flexWrap: 'wrap' }}>
        <span className="mono" style={{ fontSize: '0.66rem', letterSpacing: '0.08em', color: 'var(--ink-2)',
              border: '1px solid var(--line-2)', borderRadius: 20, padding: '5px 12px' }}>NEED CAPTURED</span>
        <span style={{ color: 'var(--ink-3)' }}>→</span>
        <span className="mono" style={{ fontSize: '0.66rem', letterSpacing: '0.08em', color: 'var(--accent-ink)',
              background: 'var(--accent)', borderRadius: 20, padding: '5px 12px', fontWeight: 600 }}>DIAGNOSIS + TASK PLAN READY</span>
      </div>
      <div style={{ padding: 14 }}>
        <Ph label="PRODUCT SCREENSHOT — diagnosis &amp; task plan view" ratio="16 / 9" grid style={{ background: 'var(--bg)' }} />
      </div>
    </div>
  );
}

/* ---------- HERO — variant dispatcher ---------- */
function Hero({ variant, go }) {
  if (variant === 'genesis') return <HeroGenesis go={go} />;
  return <HeroOutcome go={go} />;
}

/* ---------- HERO: Outcome-led (default) ---------- */
function HeroOutcome({ go }) {
  return (
    <section className="section blueprint blueprint-fade" style={{ position: 'relative', paddingTop: 'clamp(24px,5vw,56px)', paddingBottom: 'clamp(48px,7vw,88px)' }}>
      <div className="wrap" style={{ textAlign: 'center', maxWidth: 1040 }}>
        <div className="reveal" style={{ display: 'flex', justifyContent: 'center', marginBottom: 20 }}>
          <span style={{ display: 'inline-flex', alignItems: 'center', border: '1px solid var(--line)',
                borderRadius: 2, padding: '7px 14px', background: 'var(--bg)' }}>
            <span className="mono" style={{ fontSize: '0.68rem', letterSpacing: '0.28em', color: 'var(--accent)', fontWeight: 600 }}>
              KNOW FIRST
            </span>
          </span>
        </div>
        <h1 className="reveal" style={{ '--rd': '60ms', marginInline: 'auto', maxWidth: '36ch',
              fontFamily: 'var(--font-head)', fontWeight: 800, letterSpacing: '-0.03em', lineHeight: 1.08,
              fontSize: 'clamp(2.4rem,5vw,4.4rem)' }}>
          Every maintenance need, caught <span style={{ color: 'var(--accent)' }}>before</span> it becomes downtime.
        </h1>
        <p className="lead reveal" style={{ '--rd': '120ms', marginTop: 20, marginInline: 'auto', maxWidth: 640 }}>
          A maintenance need used to mean a guess, a second trip, and a part ordered twice. On Xaint, it's captured,
          diagnosed against your own documentation and history, and turned into a plan — before anyone
          even leaves for the job.
        </p>
        <div className="reveal" style={{ '--rd': '180ms', display: 'flex', justifyContent: 'center', marginTop: 26 }}>
          <Btn primary arrow href="#demo" onClick={e => { e.preventDefault(); go('demo'); }}>Request demo</Btn>
        </div>
      </div>

      <div className="wrap reveal" style={{ '--rd': '260ms', marginTop: 'clamp(32px,4vw,56px)', maxWidth: 980 }}>
        <ProductProof />
      </div>
    </section>
  );
}

/* ---------- Live AI console (self-cycling, illustrative) ---------- */
function AiConsole() {
  const STEPS = ['REGISTERING SIGNAL', 'CROSS-REFERENCING DOCUMENTATION', 'MATCHING HISTORICAL PATTERNS', 'CAUSE IDENTIFIED'];
  const DURS = [550, 700, 650, 1500];
  const [step, setStep] = useStateH(0);
  const [ms, setMs] = useStateH(0);
  useEffectH(() => {
    let raf, cancelled = false, start = performance.now(), cycleStart = start, curStep = 0, frozen = 0;
    function tick(now) {
      if (cancelled) return;
      const inStep = now - cycleStart;
      if (curStep < 3) {
        setMs(now - start);
        if (inStep >= DURS[curStep]) {
          curStep++; cycleStart = now; setStep(curStep);
          if (curStep === 3) frozen = now - start;
        }
      } else {
        setMs(frozen);
        if (inStep >= DURS[3]) { curStep = 0; cycleStart = now; start = now; setStep(0); setMs(0); }
      }
      raf = requestAnimationFrame(tick);
    }
    raf = requestAnimationFrame(tick);
    return () => { cancelled = true; cancelAnimationFrame(raf); };
  }, []);
  const secs = (ms / 1000).toFixed(2);
  return (
    <div className="card" style={{ position: 'relative', overflow: 'hidden', textAlign: 'left', background: 'var(--bg-2)' }}>
      <CornerTicks color="var(--accent)" />
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between',
           padding: '12px 16px', borderBottom: '1px solid var(--line)', gap: 12, flexWrap: 'wrap' }}>
        <span className="mono" style={{ display: 'inline-flex', alignItems: 'center', gap: 9,
              fontSize: '0.68rem', letterSpacing: '0.1em', color: 'var(--ink-2)' }}>
          <span className="dot" />XAINT AI CORE — LIVE
        </span>
        <span className="mono" style={{ fontSize: '0.58rem', color: 'var(--ink-3)', letterSpacing: '0.05em' }}>ILLUSTRATIVE — SIMULATED WALKTHROUGH</span>
      </div>
      <div style={{ padding: '22px 20px 26px' }}>
        {STEPS.map((s, i) => (
          <div key={s} style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '7px 0', opacity: i <= step ? 1 : 0.3, transition: 'opacity .3s' }}>
            <span className="mono" style={{ fontSize: '0.72rem', width: 14,
                  color: i < step ? 'var(--accent)' : i === step ? 'var(--ink)' : 'var(--ink-3)' }}>
              {i < step ? '+' : i === step ? '›' : '·'}
            </span>
            <span className="mono" style={{ fontSize: '0.78rem', letterSpacing: '0.03em',
                  color: i === 3 && step === 3 ? 'var(--accent)' : 'var(--ink-2)', fontWeight: i === 3 && step === 3 ? 700 : 500 }}>
              {s}
            </span>
          </div>
        ))}
        <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between',
             marginTop: 18, paddingTop: 18, borderTop: '1px solid var(--line)' }}>
          <span className="tag">ELAPSED</span>
          <span className="mono tabnum" style={{ fontSize: '1.5rem', fontWeight: 700, color: 'var(--accent)' }}>{secs}s</span>
        </div>
      </div>
    </div>
  );
}

/* ---------- HERO: AI & speed / genesis ---------- */
function HeroGenesis({ go }) {
  return (
    <section className="section blueprint blueprint-fade" style={{ position: 'relative', paddingTop: 'clamp(24px,5vw,56px)', paddingBottom: 'clamp(48px,7vw,88px)' }}>
      <div className="wrap" style={{ textAlign: 'center', maxWidth: 1040 }}>
        <div className="reveal" style={{ display: 'flex', justifyContent: 'center', marginBottom: 20 }}>
          <span style={{ display: 'inline-flex', alignItems: 'center', border: '1px solid var(--line)',
                borderRadius: 2, padding: '7px 14px', background: 'var(--bg)' }}>
            <span className="mono" style={{ fontSize: '0.68rem', letterSpacing: '0.24em', color: 'var(--accent)', fontWeight: 600 }}>
              THE IDEA BEHIND XAINT
            </span>
          </span>
        </div>
        <h1 className="reveal" style={{ '--rd': '60ms', marginInline: 'auto', maxWidth: '34ch',
              fontFamily: 'var(--font-head)', fontWeight: 800, letterSpacing: '-0.03em', lineHeight: 1.08,
              fontSize: 'clamp(2.4rem,5vw,4.4rem)' }}>
          Built around <span style={{ color: 'var(--accent)' }}>AI</span>. From the very first idea.
        </h1>
        <p className="lead reveal" style={{ '--rd': '120ms', marginTop: 20, marginInline: 'auto', maxWidth: 640 }}>
          Most maintenance systems bolted on automation years after they were built. Xaint started as a
          single idea: intelligence that reads every signal, cross-references your documentation and
          history, and turns it into action in under a second — not an add-on, the whole system designed
          around it from the first line of code.
        </p>
        <div className="reveal" style={{ '--rd': '180ms', display: 'flex', justifyContent: 'center', marginTop: 26 }}>
          <Btn primary arrow href="#demo" onClick={e => { e.preventDefault(); go('demo'); }}>Request demo</Btn>
        </div>
      </div>

      <div className="wrap reveal" style={{ '--rd': '260ms', marginTop: 'clamp(32px,4vw,56px)', maxWidth: 640 }}>
        <AiConsole />
      </div>
    </section>
  );
}

/* ---------- Positioning strip ---------- */
function Strip() {
  const items = ['CAPTURE → INVOICE, UNBROKEN', 'AUTO-CATEGORIZED TASKS', 'KNOWLEDGE-GROUNDED DIAGNOSIS',
    'MULTI-METER SCHEDULING', 'ONE CONNECTED PLATFORM', 'BUILT FOR SERVICE THAT BILLS'];
  const row = [...items, ...items];
  return (
    <div className="strip">
      <div className="strip-track">
        {row.map((it, i) => (
          <span key={i} className="strip-item"><span style={{ color: 'var(--accent)' }}>+</span>{it}</span>
        ))}
      </div>
    </div>
  );
}

/* ---------- Problem framing ---------- */
function Premise() {
  return (
    <section className="section">
      <div className="wrap">
        <div className="reveal" style={{ maxWidth: 880 }}>
          <Eyebrow code="00">The premise</Eyebrow>
          <h2 className="h-xl" style={{ marginTop: 22, lineHeight: 1.08 }}>
            Maintenance, captured and carried. <span className="muted">From the first signal
            to the settled invoice, in one flow.</span>
          </h2>
          <p className="lead" style={{ marginTop: 24, maxWidth: 620 }}>
            A maintenance need is captured in seconds, shaped into clear technician tasks, matched to your
            documentation, and carried through to the invoice. Xaint connects every step and automates
            the work between them — so your team always knows the state of the operation, and acts on it first.
          </p>
        </div>
      </div>
    </section>
  );
}

/* ---------- The three real differentiators ---------- */
const CLAIMS = [
  { n: 'A', big: 'Decides the work',
    h: 'It shapes the work for you.',
    p: 'A raw report becomes categorized, sequenced technician tasks automatically — so every technician opens a clear plan and starts on the right step.',
    wm: '02' },
  { n: 'B', big: 'Grounded diagnosis',
    h: 'It diagnoses from your own knowledge.',
    p: 'Diagnosis draws on your technical documentation and each asset’s own history to surface a cited, likely cause your team can act on with confidence.',
    wm: '04' },
  { n: 'C', big: 'Closes the commercial loop',
    h: 'It turns the fix into an invoice.',
    p: 'Customer, estimate, and invoice flow straight from the completed job — so maintenance contributes to revenue and pays for itself.',
    wm: '06' },
];

function Differentiators() {
  return (
    <section className="section" style={{ background: 'var(--bg-2)', borderBlock: '1px solid var(--line)' }}>
      <div className="wrap">
        <div className="reveal" style={{ display: 'flex', justifyContent: 'space-between',
             alignItems: 'flex-end', gap: 24, flexWrap: 'wrap', marginBottom: 'clamp(34px,5vw,56px)' }}>
          <div>
            <Eyebrow code="01">Why teams choose Xaint</Eyebrow>
            <h2 className="h-xl" style={{ marginTop: 20, maxWidth: '15ch' }}>Three ways Xaint moves your operation forward.</h2>
          </div>
          <p className="lead" style={{ maxWidth: 360 }}>
            One connected flow, designed around outcomes — here is the value it brings to every shift.
          </p>
        </div>
        <div className="claim-grid" style={{ display: 'grid', gridTemplateColumns: 'repeat(3,1fr)', gap: 18 }}>
          {CLAIMS.map((c, i) => (
            <article key={c.n} className="claim reveal" style={{ '--rd': `${i * 90}ms` }}>
              <CornerTicks color="var(--accent)" />
              <span className="watermark mono">{c.wm}</span>
              <div className="big">{c.big}</div>
              <h3>{c.h}</h3>
              <p>{c.p}</p>
            </article>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ---------- Roadmap teaser ---------- */
const HORIZON = [
  { st: 'SHIPPING', on: true, h: 'The full loop', p: 'Capture through invoice — the six stations, live.' },
  { st: 'IN DESIGN', on: false, h: 'Parts & procurement', p: 'Auto-reorder the part the moment the task needs it.' },
  { st: 'BUILDING', on: false, h: 'Warranty recovery', p: 'Surface what the manufacturer still owes you.' },
  { st: 'EXPLORING', on: false, h: 'Estate intelligence', p: 'Failure patterns read across every site at once.' },
];

function RoadmapTeaser({ go }) {
  return (
    <section className="section">
      <div className="wrap">
        <div className="reveal" style={{ maxWidth: 720, marginBottom: 'clamp(30px,4vw,48px)' }}>
          <Eyebrow code="02">The horizon</Eyebrow>
          <h2 className="h-xl" style={{ marginTop: 20 }}>The flow doesn't end at invoice.</h2>
          <p className="lead" style={{ marginTop: 18, maxWidth: 600 }}>
            Xaint ships as a closed loop and grows as a platform. New capability activates on the same
            flow — no migration, no second tool. A look at what's coming through it.
          </p>
        </div>
        <div className="horizon reveal">
          <div className="horizon-track">
            {HORIZON.map((h, i) => (
              <div key={i} className={`horizon-cell${h.on ? ' live' : ''}`}>
                <span className="horizon-dot" />
                <span className={`horizon-status${h.on ? ' on' : ''}`}>{h.st}</span>
                <div>
                  <h4>{h.h}</h4>
                  <p>{h.p}</p>
                </div>
              </div>
            ))}
          </div>
        </div>
        <div className="reveal" style={{ marginTop: 28 }}>
          <Btn arrow href="#roadmap" onClick={e => { e.preventDefault(); go('roadmap'); }}>See the roadmap</Btn>
        </div>
      </div>
    </section>
  );
}

/* ---------- CTA ---------- */
function CTA({ go }) {
  return (
    <section className="section">
      <div className="wrap">
        <div className="reveal blueprint" style={{ position: 'relative', border: '1px solid var(--line)',
             borderRadius: 4, padding: 'clamp(40px,7vw,92px)', textAlign: 'center', overflow: 'hidden' }}>
          <CornerTicks color="var(--accent)" inset={16} size={14} />
          <div style={{ position: 'relative' }}>
            <Eyebrow code="03">See it in your flow</Eyebrow>
            <h2 className="h-xl" style={{ marginTop: 20, marginInline: 'auto', maxWidth: '17ch' }}>
              See the whole flow on your own operation.
            </h2>
            <p className="lead" style={{ marginTop: 20, marginInline: 'auto', maxWidth: 540 }}>
              Bring a real asset and a recent maintenance need. We'll walk it through Xaint with your team — capture
              to invoice — and share a clear view of how it fits the way your operation runs.
            </p>
            <div style={{ display: 'flex', gap: 14, marginTop: 34, justifyContent: 'center', flexWrap: 'wrap' }}>
              <Btn primary arrow href="#demo" onClick={e => { e.preventDefault(); go('demo'); }}>Request demo</Btn>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

function HomePage({ t, go }) {
  const ref = useReveal();
  return (
    <main ref={ref}>
      <Hero variant={t && t.heroStyle} go={go} />
      <Strip />
      <Premise />
      <Differentiators />
      <CTA go={go} />
    </main>
  );
}

Object.assign(window, { HomePage, Eyebrow });
