/* Hi-fi — Hero block with 3 variants
 * 1. Dashboard (default) — split layout w/ live KPI dashboard
 * 2. Terminal — full-width terminal feed
 * 3. Pipeline ledger — animated bars + signal stream
 */

const Hero = ({ variant, lang, tone, accent }) => {
  const t = COPY[lang];
  const heroCopy = tone === 'fun' ? { ...t.hero, ...t.fun } : t.hero;

  return (
    <section style={{ paddingTop: 52, paddingBottom: 60, position: 'relative', overflow: 'hidden' }}>
      <svg
        aria-hidden="true"
        preserveAspectRatio="none"
        viewBox="0 0 100 100"
        style={{
          position: 'absolute',
          inset: 0,
          width: '100%',
          height: '100%',
          pointerEvents: 'none',
          zIndex: 0
        }}
      >
        <defs>
          <linearGradient id="hero-ray-fade" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="50">
            <stop offset="0%" stopColor="rgb(28,29,31)" stopOpacity="0.05" />
            <stop offset="100%" stopColor="rgb(28,29,31)" stopOpacity="0" />
          </linearGradient>
        </defs>
        {[-3, -1, 1, 3].map(i => (
          <line
            key={i}
            x1="50"
            y1="130"
            x2={50 + i * 18}
            y2="-10"
            stroke="url(#hero-ray-fade)"
            strokeWidth="1"
            vectorEffect="non-scaling-stroke"
          />
        ))}
      </svg>
      <div className="u-container" style={{ position: 'relative', zIndex: 1 }}>
        {variant === 'dashboard' && <HeroDashboard t={t} h={heroCopy} />}
        {variant === 'terminal' && <HeroTerminal t={t} h={heroCopy} />}
        {variant === 'ledger' && <HeroLedger t={t} h={heroCopy} />}
      </div>
    </section>
  );
};

// ----------------- Hero variant 1 — split + dashboard
const MobileMetricTicker = ({ metrics }) => {
  const [idx, setIdx] = useState(0);
  const [visible, setVisible] = useState(true);

  useEffect(() => {
    const id = setInterval(() => {
      setVisible(false);
      setTimeout(() => {
        setIdx(i => (i + 1) % metrics.length);
        setVisible(true);
      }, 320);
    }, 2600);
    return () => clearInterval(id);
  }, [metrics.length]);

  const m = metrics[idx];
  return (
    <div className="u-mobile-only" style={{ textAlign: 'center', marginTop: 18 }}>
      <span style={{
        display: 'inline-flex', alignItems: 'baseline', gap: 8,
        opacity: visible ? 1 : 0,
        transition: 'opacity 0.32s ease',
      }}>
        <span style={{
          fontFamily: 'var(--font-mono)', fontWeight: 700,
          fontSize: 13, color: 'var(--ink)',
        }}>{m.v}</span>
        <span style={{ fontSize: 13, color: 'var(--gray-3)' }}>{m.l}</span>
      </span>
    </div>
  );
};

const HeroDashboard = ({ t, h }) => {
  return (
    <div style={{ display: 'grid', gridTemplateColumns: '1.05fr 1fr', gap: 60, alignItems: 'center' }}>
      <div>
        <h1 style={{ marginBottom: 24 }}>
          {h.h1a}<br/>{h.h1b} <span className="u-mark">{h.h1mark}</span>{h.h1c}
        </h1>
        <p style={{ fontSize: 19, lineHeight: 1.55, color: 'var(--gray-4)', maxWidth: 480, marginBottom: 32 }}>
          {h.sub}
        </p>
        <div style={{ display: 'flex', gap: 12, marginBottom: 28 }}>
          <a className="u-btn u-btn-primary" data-cal-link="antoineguine/appel-de-decouverte-website" data-cal-namespace="appel-de-decouverte-website" data-cal-config='{"layout":"month_view","useSlotsViewOnSmallScreen":"true"}' style={{ padding: '14px 24px', fontSize: 16, cursor: 'pointer' }}>{h.ctaA} →</a>
          <a className="u-btn u-btn-ghost u-desktop-only" href="#/use-cases" onClick={(e) => { e.preventDefault(); window.location.hash = '/use-cases'; window.scrollTo({ top: 0, behavior: 'auto' }); }} style={{ padding: '14px 22px', fontSize: 16, cursor: 'pointer' }}>{h.ctaB}</a>
        </div>
        <MobileMetricTicker metrics={t.metrics} />
        <div className="u-desktop-only" style={{ display: 'flex', alignItems: 'center', gap: 14, marginTop: 0 }}>
          <div style={{ display: 'flex' }}>
            {[0,1,2,3,4].map(i => (
              <div key={i} style={{
                width: 28, height: 28, borderRadius: '50%', marginLeft: i ? -10 : 0,
                background: ['#D4C5A0','#A8A398','#8B8A82','#6F6B62','#3F3D38'][i],
                border: '2px solid var(--bg)',
              }}></div>
            ))}
          </div>
          <p style={{ fontSize: 13, color: 'var(--gray-3)' }}>{h.proof}</p>
        </div>
      </div>
      <DashboardMockup />
    </div>
  );
};

// ----------------- Dashboard mockup with live counters + scrolling feed
const DashboardMockup = () => {
  const [pipeline, setPipeline] = useState(847234);
  const [meetings, setMeetings] = useState(23);
  const [reply, setReply] = useState(14.3);

  useEffect(() => {
    const id = setInterval(() => {
      setPipeline(p => p + Math.floor(Math.random() * 1200));
      if (Math.random() > 0.7) setMeetings(m => m + 1);
      setReply(r => Math.max(11.5, Math.min(16.5, r + (Math.random() - 0.5) * 0.4)));
    }, 2200);
    return () => clearInterval(id);
  }, []);

  return (
    <div className="u-card" style={{ padding: 18, borderRadius: 18, position: 'relative', background: 'var(--paper)' }}>
      {/* glow */}
      <div style={{
        position: 'absolute', inset: -1, borderRadius: 18,
        background: 'linear-gradient(135deg, var(--accent) 0%, transparent 50%)',
        opacity: 0.18, pointerEvents: 'none', zIndex: 0,
      }}></div>

      <div style={{ position: 'relative', zIndex: 1 }}>
        {/* header */}
        <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 12, paddingBottom: 12, borderBottom: '1px solid var(--line)' }}>
          <div style={{ width: 22, height: 22, borderRadius: 6, background: 'var(--ink)' }}></div>
          <span style={{ fontSize: 13, fontWeight: 600 }}>upscale</span>
          <span className="u-mono" style={{ fontSize: 11, color: 'var(--gray-3)', marginLeft: 4 }}>/ pipeline · Q2</span>
          <span style={{ marginLeft: 'auto', display: 'flex', alignItems: 'center', gap: 6, fontSize: 11, color: 'var(--positive)' }}>
            <span className="u-dot u-dot-live"></span>
            live
          </span>
        </div>

        {/* big metric */}
        <div style={{ padding: '14px 16px', background: 'var(--bg)', borderRadius: 12, marginBottom: 10, border: '1px solid var(--line)' }}>
          <p style={{ fontSize: 11, color: 'var(--gray-3)', textTransform: 'uppercase', letterSpacing: '0.06em', fontFamily: 'var(--font-mono)' }}>Pipeline généré</p>
          <div style={{ display: 'flex', alignItems: 'baseline', gap: 10, marginTop: 4 }}>
            <span style={{ fontFamily: 'var(--font-display)', fontSize: 44, lineHeight: 1, fontWeight: 400, fontStyle: 'italic' }}>
              €{pipeline.toLocaleString('fr-FR')}
            </span>
            <span className="u-chip u-chip-accent" style={{ fontSize: 11, padding: '2px 8px' }}>+74%</span>
          </div>
          {/* sparkline */}
          <svg viewBox="0 0 320 50" style={{ width: '100%', height: 50, marginTop: 8 }}>
            <defs>
              <linearGradient id="sparkFill" x1="0" x2="0" y1="0" y2="1">
                <stop offset="0%" stopColor="var(--accent-edge)" stopOpacity="0.35"/>
                <stop offset="100%" stopColor="var(--accent-edge)" stopOpacity="0"/>
              </linearGradient>
            </defs>
            <polyline points="0,42 30,38 60,40 90,30 120,32 150,22 180,25 210,15 240,18 270,12 300,8 320,4" fill="none" stroke="var(--accent-edge)" strokeWidth="1.6" />
            <polyline points="0,42 30,38 60,40 90,30 120,32 150,22 180,25 210,15 240,18 270,12 300,8 320,4 320,50 0,50" fill="url(#sparkFill)" stroke="none" />
          </svg>
        </div>

        {/* 3 mini metrics */}
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 8, marginBottom: 12 }}>
          {[
            { l: 'RDV bookés', v: meetings, delta: '+3' },
            { l: 'Reply rate', v: reply.toFixed(1) + '%', delta: '+2.1', accent: true },
            { l: 'Délivrabilité', v: '97.2%', delta: 'stable' },
          ].map((m, i) => (
            <div key={i} style={{ padding: '10px 12px', background: 'var(--bg)', borderRadius: 10, border: '1px solid var(--line)' }}>
              <p style={{ fontSize: 10, color: 'var(--gray-3)', fontFamily: 'var(--font-mono)' }}>{m.l}</p>
              <p style={{ fontSize: 22, fontWeight: 600, marginTop: 2, color: m.accent ? 'var(--accent-edge)' : 'var(--ink)' }}>{m.v}</p>
              <p style={{ fontSize: 10, color: 'var(--gray-3)', marginTop: 2 }}>{m.delta}</p>
            </div>
          ))}
        </div>

        {/* live feed */}
        <p style={{ fontSize: 11, color: 'var(--gray-3)', fontFamily: 'var(--font-mono)', textTransform: 'uppercase', letterSpacing: '0.06em', margin: '6px 4px 8px', display: 'flex', alignItems: 'center', gap: 6 }}>
          <span className="u-dot u-dot-live"></span> Signaux · live
        </p>
        <ScrollingFeed />
      </div>
    </div>
  );
};

// ----------------- Scrolling signal feed
const FEED_ITEMS = [
  { sig: 'funding_round', co: 'Acme Robotics', det: '$12M Series A', score: '+18' },
  { sig: 'new_vp_sales', co: 'Pylon SaaS', det: 'ex-Datadog', score: '+22' },
  { sig: 'hiring_sales', co: 'Lumen Health', det: '2 BDRs · SF', score: '+14' },
  { sig: 'stack_swap', co: 'Northwind', det: 'HS → Salesforce', score: '+16' },
  { sig: 'champion → cro', co: 'Forge.ai', det: 'ex-champ now CRO', score: '+24' },
  { sig: 'soc2_obtained', co: 'Vault Systems', det: 'press release', score: '+9' },
  { sig: 'product_launch', co: 'Beacon', det: 'PH #2 today', score: '+12' },
  { sig: 'job_change', co: 'Helix', det: 'CMO ex-Cleaq', score: '+19' },
];

const ScrollingFeed = () => {
  const [items, setItems] = useState(FEED_ITEMS.slice(0, 4));
  const [tick, setTick] = useState(0);

  useEffect(() => {
    const id = setInterval(() => {
      setTick(t => t + 1);
      setItems(prev => {
        const next = FEED_ITEMS[(tick + 4) % FEED_ITEMS.length];
        return [{ ...next, _id: Date.now() }, ...prev.slice(0, 3)];
      });
    }, 2400);
    return () => clearInterval(id);
  }, [tick]);

  return (
    <div style={{ overflow: 'hidden', height: 168 }}>
      {items.map((r, i) => (
        <div key={r._id || i} style={{
          padding: '8px 10px',
          borderRadius: 8,
          marginBottom: 4,
          background: i === 0 ? 'rgba(225,255,108,0.18)' : 'transparent',
          border: i === 0 ? '1px solid var(--accent-edge)' : '1px solid transparent',
          display: 'flex', alignItems: 'center', gap: 10,
          animation: i === 0 ? 'feedSlideIn 0.5s ease-out' : 'none',
        }}>
          <span className="u-mono" style={{ fontSize: 10, color: 'var(--gray-3)', flex: '0 0 110px', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{r.sig}</span>
          <span style={{ fontSize: 12, fontWeight: 600, flex: '0 0 96px', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{r.co}</span>
          <span style={{ fontSize: 11, color: 'var(--gray-3)', flex: 1, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{r.det}</span>
          <span className="u-chip u-chip-accent" style={{ fontSize: 10, padding: '1px 6px' }}>{r.score}</span>
        </div>
      ))}
    </div>
  );
};

// ----------------- Hero variant 2 — Terminal
const HeroTerminal = ({ t, h }) => {
  return (
    <div>
      <div style={{ textAlign: 'center', maxWidth: 920, margin: '0 auto 60px' }}>
        <h1 style={{ marginBottom: 24, fontSize: 72 }}>
          {h.h2a}<br/>{h.h2b} <span className="u-mark">{h.h2mark}</span>{h.h2c}
        </h1>
        <p style={{ fontSize: 19, lineHeight: 1.55, color: 'var(--gray-4)', maxWidth: 580, margin: '0 auto 32px' }}>
          {h.sub}
        </p>
        <div style={{ display: 'flex', gap: 12, justifyContent: 'center', flexWrap: 'wrap' }}>
          <a className="u-btn u-btn-primary" data-cal-link="antoineguine/appel-de-decouverte-website" data-cal-namespace="appel-de-decouverte-website" data-cal-config='{"layout":"month_view","useSlotsViewOnSmallScreen":"true"}' style={{ padding: '14px 24px', fontSize: 16, cursor: 'pointer' }}>{h.ctaA} →</a>
          <a className="u-btn u-btn-ghost u-desktop-only" href="#/use-cases" onClick={(e) => { e.preventDefault(); window.location.hash = '/use-cases'; window.scrollTo({ top: 0, behavior: 'auto' }); }} style={{ padding: '14px 22px', fontSize: 16, cursor: 'pointer' }}>{h.ctaB}</a>
        </div>
        <div className="u-desktop-only" style={{ display: 'flex', gap: 28, justifyContent: 'center', marginTop: 22, flexWrap: 'wrap' }}>
          {t.metrics.map((m, i) => (
            <div key={i} style={{ display: 'flex', alignItems: 'baseline', gap: 6, fontSize: 13 }}>
              <span style={{ fontWeight: 600, fontFamily: 'var(--font-mono)', color: 'var(--ink)' }}>{m.v}</span>
              <span style={{ color: 'var(--gray-3)' }}>{m.l}</span>
            </div>
          ))}
        </div>
        <MobileMetricTicker metrics={t.metrics} />
      </div>
      {/* <HeroVideo /> — désactivée temporairement, réactiver quand prête */}
    </div>
  );
};

// ----------------- Hero video — Tella embed
const HeroVideo = () => {
  return (
    <div className="u-card" style={{
      padding: 0,
      borderRadius: 18,
      overflow: 'hidden',
      background: 'var(--ink)',
      borderColor: 'var(--ink)',
      boxShadow: 'var(--shadow-strong)',
      position: 'relative',
      maxWidth: 880,
      margin: '0 auto',
    }}>
      <div style={{ position: 'relative', width: '100%', paddingTop: '56.25%' }}>
        <iframe
          src="https://www.tella.tv/video/upscale-setting-8ewb/embed?b=0&title=0&a=0&loop=0&t=0&muted=0&wt=0"
          allow="autoplay; fullscreen; picture-in-picture"
          allowFullScreen
          style={{
            position: 'absolute', top: 0, left: 0,
            width: '100%', height: '100%',
            border: 0,
          }}
          title="Upscale — démo"
        />
      </div>
    </div>
  );
};

const TerminalLines = [
  { type: 'cmd', t: '$ upscale watch --signals=funding,hiring,jobchange' },
  { type: 'info', t: '→ tracking 2,847 accounts · 12 signal types · 4 active sequences' },
  { type: 'ok', t: '[14:02] ✓ Cleaq · raised $12M Series A · sequence_03 sent' },
  { type: 'ok', t: '[14:03] ✓ Delos · hired VP Sales · enriched · routed to marion@' },
  { type: 'ok', t: '[14:04] ✓ Review Collect · 2 BDR posts · scoring +18 · in queue' },
  { type: 'metric', t: '[14:04] ↑ pipeline_value += €34,200' },
  { type: 'ok', t: '[14:05] ✓ Alteia · stack swap HS → SF · sequence_07 triggered' },
  { type: 'ok', t: '[14:05] ✓ Kalent · champion promoted to CRO · score +24 · hot' },
  { type: 'metric', t: '[14:06] ↑ booked_meeting · Hyperstack · Tue 16:30' },
];

const TerminalMockup = () => {
  const [visible, setVisible] = useState(0);
  useEffect(() => {
    const id = setInterval(() => setVisible(v => Math.min(TerminalLines.length, v + 1)), 380);
    return () => clearInterval(id);
  }, []);

  return (
    <div className="u-card" style={{ padding: 0, borderRadius: 14, overflow: 'hidden', background: 'var(--ink)', borderColor: 'var(--ink)' }}>
      <div style={{ padding: '10px 16px', borderBottom: '1px solid rgba(255,255,255,0.08)', display: 'flex', alignItems: 'center', gap: 8 }}>
        <span style={{ width: 12, height: 12, borderRadius: '50%', background: '#ff5f57' }}></span>
        <span style={{ width: 12, height: 12, borderRadius: '50%', background: '#febc2e' }}></span>
        <span style={{ width: 12, height: 12, borderRadius: '50%', background: '#28c840' }}></span>
        <span style={{ marginLeft: 14, fontSize: 12, color: 'rgba(255,253,246,0.5)', fontFamily: 'var(--font-mono)' }}>~/upscale · live signals</span>
        <span style={{ marginLeft: 'auto', fontSize: 11, color: 'var(--accent)', display: 'flex', alignItems: 'center', gap: 6 }}>
          <span className="u-dot" style={{ background: 'var(--accent)' }}></span>running
        </span>
      </div>
      <div style={{ padding: '20px 24px', fontFamily: 'var(--font-mono)', fontSize: 13, lineHeight: 1.85, minHeight: 260 }}>
        {TerminalLines.slice(0, visible).map((l, i) => (
          <div key={i} style={{
            color: l.type === 'cmd' ? 'rgba(255,253,246,0.45)' :
                  l.type === 'info' ? 'var(--accent)' :
                  l.type === 'metric' ? 'rgba(255,253,246,0.6)' :
                  'rgba(255,253,246,0.92)',
            animation: 'feedSlideIn 0.3s ease',
          }}>
            {l.t.split(/(\[\d{2}:\d{2}\])/).map((part, j) => (
              part.match(/\[\d{2}:\d{2}\]/) ? <span key={j} style={{ color: 'var(--accent)' }}>{part}</span> : <span key={j}>{part}</span>
            ))}
          </div>
        ))}
        {visible >= TerminalLines.length && (
          <div style={{ color: 'rgba(255,253,246,0.45)', marginTop: 8 }}>
            $ <span style={{ background: 'var(--accent)', color: 'var(--ink)', padding: '0 4px', animation: 'pulseRing 1.5s infinite' }}>_</span>
          </div>
        )}
      </div>
    </div>
  );
};

// ----------------- Hero variant 3 — Pipeline ledger
const HeroLedger = ({ t, h }) => {
  return (
    <div style={{ display: 'grid', gridTemplateColumns: '1fr 1.1fr', gap: 60, alignItems: 'center' }}>
      <div>
        <span className="u-chip" style={{ marginBottom: 22 }}>
          <span className="u-dot u-dot-live"></span>
          {h.pill}
        </span>
        <h1 style={{ marginBottom: 24, fontSize: 76 }}>
          {h.h3a} <span className="u-mark">{h.h3mark}</span>{h.h3b}<br/>
          <span style={{ color: 'var(--gray-3)' }}>{h.h3c}</span>
        </h1>
        <p style={{ fontSize: 18, lineHeight: 1.55, color: 'var(--gray-4)', maxWidth: 460, marginBottom: 32 }}>
          {h.sub}
        </p>
        <div style={{ display: 'flex', gap: 12 }}>
          <a className="u-btn u-btn-primary" data-cal-link="antoineguine/appel-de-decouverte-website" data-cal-namespace="appel-de-decouverte-website" data-cal-config='{"layout":"month_view","useSlotsViewOnSmallScreen":"true"}' style={{ cursor: 'pointer' }}>{h.ctaA} →</a>
          <a className="u-btn u-btn-ghost" href="#/use-cases" onClick={(e) => { e.preventDefault(); window.location.hash = '/use-cases'; window.scrollTo({ top: 0, behavior: 'auto' }); }} style={{ cursor: 'pointer' }}>{h.ctaB}</a>
        </div>
      </div>
      <PipelineLedgerMockup />
    </div>
  );
};

const PipelineLedgerMockup = () => {
  const data = [42, 56, 38, 72, 65, 88, 76, 94, 82, 110, 98, 125];
  const [hover, setHover] = useState(11);
  return (
    <div className="u-card" style={{ padding: 22, borderRadius: 18 }}>
      <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', marginBottom: 18 }}>
        <div>
          <p style={{ fontSize: 11, color: 'var(--gray-3)', fontFamily: 'var(--font-mono)', textTransform: 'uppercase', letterSpacing: '0.06em' }}>Pipeline généré · 12 mois</p>
          <p style={{ fontFamily: 'var(--font-display)', fontStyle: 'italic', fontSize: 48, lineHeight: 1, marginTop: 6 }}>
            €<AnimatedNumber to={1247} suffix="k" />
          </p>
        </div>
        <span className="u-chip u-chip-accent">+74%</span>
      </div>
      <div style={{ display: 'flex', alignItems: 'flex-end', gap: 6, height: 180, padding: '0 4px', marginBottom: 14 }}>
        {data.map((h, i) => (
          <div key={i}
            onMouseEnter={() => setHover(i)}
            style={{
              flex: 1,
              height: `${(h / 125) * 100}%`,
              background: hover === i ? 'var(--accent)' : 'var(--bg-2)',
              borderRadius: '3px 3px 0 0',
              transition: 'background 0.2s, height 0.6s cubic-bezier(0.34, 1.56, 0.64, 1)',
              animation: `barGrow 1s ease-out ${i * 0.06}s backwards`,
              border: hover === i ? '1px solid var(--accent-edge)' : 'none',
              cursor: 'pointer',
            }}></div>
        ))}
      </div>
      <div style={{ display: 'flex', justifyContent: 'space-between', fontSize: 11, color: 'var(--gray-3)', fontFamily: 'var(--font-mono)' }}>
        <span>Mai 25</span>
        <span>Avr 26</span>
      </div>
    </div>
  );
};

window.Hero = Hero;
