/* global React */
// Hero — the headline moment.
function Hero({ t = {} }) {
  const DS = window.ReconcileAIDesignSystem_c7572b;
  const { ExceptionCard, Badge } = DS;

  const ALL_CARDS = [
    { merchant: 'Amazon Web Services', amount: '£1,248.00', date: '2026-06-03 · 09:14', reason: 'No matching invoice found, but the amount and timing match your usual monthly cloud bill. Most likely AWS hosting.', suggestedAccount: 'Software & hosting', confidence: 96, pastTransactions: [{ date: '2026-05-03', amount: '£1,248.00' }, { date: '2026-04-03', amount: '£1,248.00' }, { date: '2026-03-03', amount: '£1,198.00' }] },
    { merchant: 'Stripe Inc.', amount: '£320.50', date: '2026-06-02 · 14:22', reason: "Payment processor fee matching your typical monthly Stripe volume charge. Consistent with last quarter's figures.", suggestedAccount: 'Payment processing', confidence: 94, pastTransactions: [{ date: '2026-05-02', amount: '£298.60' }, { date: '2026-04-02', amount: '£312.20' }] },
    { merchant: 'Notion Labs', amount: '£84.00', date: '2026-06-01 · 11:05', reason: "Annual team workspace renewal. Same amount and merchant as last year's subscription — high confidence auto-match.", suggestedAccount: 'Software & hosting', confidence: 97, pastTransactions: [{ date: '2025-06-01', amount: '£84.00' }] },
    { merchant: 'WeWork', amount: '£2,100.00', date: '2026-05-31 · 08:00', reason: 'Monthly co-working desk rental matching your office space contract. Recurring charge on the first of every month.', suggestedAccount: 'Rent & facilities', confidence: 92, pastTransactions: [{ date: '2026-05-01', amount: '£2,100.00' }, { date: '2026-04-01', amount: '£2,100.00' }, { date: '2026-03-01', amount: '£2,100.00' }] },
    { merchant: 'Google Cloud', amount: '£567.80', date: '2026-05-30 · 10:30', reason: 'Cloud compute and storage charges consistent with engineering team usage. Aligns with your active GCP project billing.', suggestedAccount: 'Software & hosting', confidence: 91, pastTransactions: [{ date: '2026-04-30', amount: '£489.20' }, { date: '2026-03-30', amount: '£531.60' }] },
  ];

  const [currentIdx, setCurrentIdx]   = React.useState(0);
  const [animKey, setAnimKey]         = React.useState(0);
  const [isExiting, setIsExiting]     = React.useState(false);
  const [isInteracting, setInteracting] = React.useState(false);
  const advancingRef                  = React.useRef(false);
  const hadInteractionRef             = React.useRef(false);

  // Two-phase transition: fade out current card, then swap + fade in new card
  const goNext = React.useCallback(() => {
    if (advancingRef.current) return;
    advancingRef.current = true;
    setIsExiting(true);
    setTimeout(() => {
      setCurrentIdx(i => (i + 1) % ALL_CARDS.length);
      setAnimKey(k => k + 1);
      setIsExiting(false);
      advancingRef.current = false;
    }, 520);
  }, []);

  // Pause while the user has the dropdown or review panel open.
  // When they close it, wait 1 s then advance — instead of the usual 5 s.
  React.useEffect(() => {
    if (isInteracting) {
      hadInteractionRef.current = true;
      return;
    }
    const delay = hadInteractionRef.current ? 1000 : 5000;
    hadInteractionRef.current = false;
    const timer = setTimeout(goNext, delay);
    return () => clearTimeout(timer);
  }, [currentIdx, isInteracting, goNext]);

  React.useEffect(() => { requestAnimationFrame(() => window.lucide && window.lucide.createIcons()); });

  // On manual resolve: clear any lingering interaction state, show "Reconciled" briefly,
  // then swap in the next card without the exit animation.
  const handleManualResolve = () => {
    if (advancingRef.current) return;
    advancingRef.current = true;
    setInteracting(false);
    setTimeout(() => {
      setCurrentIdx(i => (i + 1) % ALL_CARDS.length);
      setAnimKey(k => k + 1);
      advancingRef.current = false;
    }, 650);
  };

  const card = ALL_CARDS[currentIdx];

  return (
    <section style={{ position: 'relative', overflow: 'hidden' }}>
      <div aria-hidden="true" style={{
        position: 'absolute', inset: 0,
        background: 'radial-gradient(1100px 520px at 72% -8%, var(--cyan-50), transparent 60%)',
        pointerEvents: 'none',
      }} />

      <style>{`
        @keyframes heroCardOut {
          from { opacity: 1; transform: translateY(0);   }
          to   { opacity: 0; transform: translateY(-10px); }
        }
        @keyframes heroCardIn {
          from { opacity: 0; transform: translateY(12px); }
          to   { opacity: 1; transform: translateY(0);   }
        }
        .hero-card-exit  { animation: heroCardOut 0.5s  ease-in  forwards; pointer-events: none; }
        .hero-card-enter { animation: heroCardIn  0.65s ease-out forwards; }
      `}</style>

      <div style={{
        position: 'relative', maxWidth: 'var(--container-max)', margin: '0 auto',
        padding: 'clamp(3rem,7vw,5.5rem) var(--container-pad) var(--section-y)',
        display: 'grid', gridTemplateColumns: '1.05fr 0.95fr', gap: 'clamp(2rem,5vw,4.5rem)',
        alignItems: 'flex-start',
      }} className="rai-hero-grid">

        {/* Left: copy */}
        <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start', gap: '24px' }}>
          <Badge tone="brand" variant="soft" size="lg" icon={<i data-lucide="brain-circuit"></i>}>
            Every exception, explained
          </Badge>
          <h1 style={{
            fontSize: 'clamp(2.6rem, 5.2vw, 4.4rem)', fontWeight: 800,
            letterSpacing: '-0.035em', lineHeight: 1.02, color: 'var(--ink-900)', margin: 0,
          }}>
            The <span style={{ color: 'var(--cyan-600)' }}>5%</span> reconciliation<br />software misses out on.
          </h1>
          <p style={{
            fontSize: 'clamp(1.05rem,1.6vw,1.25rem)', lineHeight: 1.55, color: 'var(--text-muted)',
            maxWidth: '34ch', margin: 0,
          }}>
            Your books get 95% of the way. ReconcileAI clears the rest — every exception explained in
            plain English, resolved in one tap, and categorised for you.
          </p>
          <div style={{ marginTop: '4px', width: '100%', minHeight: '96px' }}>
            <WaitlistForm
              id="waitlist"
              ctaLabel={t.ctaLabel || 'Join waitlist'}
              placeholder={t.placeholder || 'you@company.com'}
              requireBusiness={t.requireBusiness !== false}
              successMessage={t.successMessage || "You're on the list — we'll be in touch."}
              showCount={!!t.showCount}
            />
          </div>
          <div style={{ display: 'flex', alignItems: 'center', gap: '10px', marginTop: '2px', color: 'var(--text-subtle)' }}>
            <i data-lucide="lock" style={{ width: 15, height: 15 }}></i>
            <span style={{ fontSize: '13.5px' }}>Enterprise-grade security · Be first in when we launch</span>
          </div>
        </div>

        {/* Right: stacked slideshow */}
        <div style={{ position: 'relative', alignSelf: 'center' }}>

          {/* Ghost depth cards */}
          <div aria-hidden="true" style={{
            position: 'absolute', top: 14, left: 10, right: -10, bottom: -14,
            background: 'var(--surface-card)', borderRadius: 'var(--radius-xl)',
            boxShadow: 'var(--shadow-sm)', opacity: 0.32, zIndex: 1,
          }} />
          <div aria-hidden="true" style={{
            position: 'absolute', top: 7, left: 5, right: -5, bottom: -7,
            background: 'var(--surface-card)', borderRadius: 'var(--radius-xl)',
            boxShadow: 'var(--shadow-sm)', opacity: 0.58, zIndex: 2,
          }} />

          {/* Active card */}
          <div style={{ position: 'relative', zIndex: 3, display: 'flex', flexDirection: 'column', gap: '14px' }}>
            <div key={animKey} className={isExiting ? 'hero-card-exit' : 'hero-card-enter'}>
              <ExceptionCard
                {...card}
                onResolve={handleManualResolve}
                onInteractionStart={() => setInteracting(true)}
                onInteractionEnd={() => setInteracting(false)}
              />
            </div>

            {/* Dot progress indicators */}
            <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', gap: '6px', marginTop: '2px' }}>
              {ALL_CARDS.map((_, i) => (
                <div key={i} style={{
                  height: 6,
                  width: i === currentIdx ? 22 : 6,
                  borderRadius: 3,
                  background: i === currentIdx ? 'var(--brand)' : 'var(--border-default)',
                  transition: 'width 0.4s ease-out, background 0.3s',
                }} />
              ))}
            </div>
          </div>
        </div>

      </div>
    </section>
  );
}
window.Hero = Hero;
