// Caliber demo embed sections — wraps the live demo at caliber-demo.vercel.app
// in a polished presentation shell for use across marketing variants.

const DEMO_URL = 'https://caliber-demo.vercel.app/';

/* =========================================================
   BrowserChrome — styled window frame wrapping any iframe
   ========================================================= */
function BrowserChrome({ url = 'caliber-demo.vercel.app', dark = false, children, height = 760, style = {} }) {
  return (
    <div style={{
      borderRadius: 16, overflow: 'hidden',
      background: dark ? '#111111' : '#fff',
      border: `1px solid ${dark ? 'rgba(255,255,255,0.12)' : 'var(--cal-line-2)'}`,
      boxShadow: dark
        ? '0 30px 60px -20px rgba(0,9,182,0.35), 0 8px 24px rgba(0,0,0,0.4)'
        : 'var(--shadow-lg)',
      ...style,
    }}>
      {/* chrome bar */}
      <div style={{
        display: 'flex', alignItems: 'center', gap: 10,
        padding: '10px 14px',
        background: dark ? '#0A0A0B' : '#F3F4F6',
        borderBottom: `1px solid ${dark ? 'rgba(255,255,255,0.08)' : 'var(--cal-line-2)'}`,
      }}>
        {/* traffic lights */}
        <div style={{ display: 'flex', gap: 6 }}>
          <span style={{ width: 12, height: 12, borderRadius: 999, background: '#FF5F57' }} />
          <span style={{ width: 12, height: 12, borderRadius: 999, background: '#FEBC2E' }} />
          <span style={{ width: 12, height: 12, borderRadius: 999, background: '#28C840' }} />
        </div>
        {/* url pill */}
        <div style={{
          flex: 1, textAlign: 'center',
          background: dark ? 'rgba(255,255,255,0.06)' : '#fff',
          color: dark ? 'rgba(255,255,255,0.7)' : 'var(--cal-muted)',
          border: `1px solid ${dark ? 'rgba(255,255,255,0.08)' : 'var(--cal-line-2)'}`,
          borderRadius: 999,
          padding: '5px 14px',
          fontSize: 12,
          fontFamily: 'ui-monospace, "SF Mono", Menlo, monospace',
          maxWidth: 360, margin: '0 auto',
          display: 'inline-flex', alignItems: 'center', gap: 6, justifyContent: 'center',
        }}>
          {Icon.lock({ size: 11 })} {url}
        </div>
        <div style={{ width: 54 }} />
      </div>
      <div style={{ height, background: dark ? '#111' : '#fff', position: 'relative' }}>
        {children}
      </div>
    </div>
  );
}

/* =========================================================
   LiveDemo — standard "Try Caliber" section used by Variants A–F.
   Shows a headline + perks on the left, the iframe on the right.
   ========================================================= */
function LiveDemo({
  eyebrow = 'Live product',
  title,
  sub = 'No demo call, no login. This is the real product with a sample agency pre-loaded. Poke around — nothing will break.',
  variant = 'split',   // 'split' | 'stacked' | 'hero'
  theme = 'light',
  showBadges = true,
}) {
  const dark = theme === 'dark';
  const [iframeReady, setIframeReady] = React.useState(false);
  const ref = React.useRef(null);
  const inView = useInView(ref, 0.15);

  // Lazy-load the iframe so it doesn't slow the page
  const shouldLoad = inView;

  const Frame = (
    <div ref={ref} style={{ position: 'relative' }}>
      <BrowserChrome url="app.caliberjobs.com" dark={dark} height={variant === 'hero' ? 820 : 720}>
        {!iframeReady && (
          <div style={{
            position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center',
            background: dark ? '#0A0A0B' : '#F7F8FB', flexDirection: 'column', gap: 16,
            color: dark ? 'rgba(255,255,255,0.6)' : 'var(--cal-muted)',
          }}>
            <div style={{
              width: 44, height: 44,
              background: 'conic-gradient(from 0deg, #0084FF, #0009B6, #0084FF)',
              borderRadius: 999,
              mask: 'radial-gradient(circle, transparent 62%, #000 64%)',
              WebkitMask: 'radial-gradient(circle, transparent 62%, #000 64%)',
              animation: 'cal-star-spin 1.1s linear infinite',
            }} />
            <div style={{ fontSize: 13 }}>
              {shouldLoad ? 'Booting Caliber…' : 'Ready when you are — scroll into view'}
            </div>
          </div>
        )}
        {shouldLoad && (
          <iframe
            src={DEMO_URL}
            title="Caliber live demo"
            onLoad={() => setIframeReady(true)}
            style={{
              width: '100%', height: '100%', border: 'none', display: 'block',
              opacity: iframeReady ? 1 : 0, transition: 'opacity 0.4s',
              background: '#fff',
            }}
            loading="lazy"
          />
        )}
      </BrowserChrome>

      {/* S2.3 — "Live · fully interactive" badge removed (gilded; the try-these
          pills above the iframe already carry the interactive signal). */}
    </div>
  );

  if (variant === 'hero') {
    return <div style={{ maxWidth: 1280, margin: '0 auto' }}>{Frame}</div>;
  }

  // Default: a full-width showcase. Copy sits above as a compact header
  // strip, the product window below runs the full content width.
  return (
    <section id="demo" data-screen-label="Live demo" style={{
      padding: '120px 72px',
      background: dark ? 'var(--cal-ink)' : 'var(--cal-bg-soft)',
      color: dark ? '#fff' : 'var(--cal-ink)',
      position: 'relative', overflow: 'hidden',
    }}>
      <div className="cal-container">
        {/* Header strip — eyebrow left, heading + sub + CTAs spanning right */}
        <div style={{
          display: 'grid', gridTemplateColumns: '1fr 1.4fr', gap: 56,
          alignItems: 'end', marginBottom: 48,
        }}>
          <div>
            <div className="cal-eyebrow" style={{
              marginBottom: 18,
              background: dark ? 'rgba(255,255,255,0.08)' : 'var(--cal-blue-50)',
              color: dark ? 'rgba(255,255,255,0.85)' : 'var(--cal-blue-ink)',
              border: `1px solid ${dark ? 'rgba(255,255,255,0.12)' : 'rgba(1,65,215,0.15)'}`,
            }}>
              <span className="dot" style={{ background: '#4ADE80', boxShadow: '0 0 0 3px rgba(74,222,128,0.22)' }} />
              {eyebrow}
            </div>
            <h2 style={{ color: dark ? '#fff' : 'var(--cal-ink)', margin: 0 }}>
              {title || <>Don&rsquo;t take our word for it. <span style={{ fontFamily: 'var(--cal-serif)', fontStyle: 'italic', fontWeight: 500, fontVariationSettings: '"opsz" 72, "SOFT" 100, "WONK" 0, "wght" 500', color: 'var(--cal-accent)' }}>Try it.</span></>}
            </h2>
          </div>
          <div>
            <p style={{ fontSize: 18, lineHeight: 1.55, color: dark ? 'rgba(255,255,255,0.8)' : 'var(--cal-body)', margin: '0 0 20px', maxWidth: 640 }}>
              {sub}
            </p>
            <div style={{ display: 'flex', gap: 10, flexWrap: 'wrap', alignItems: 'center' }}>
              <a href={DEMO_URL} target="_blank" rel="noreferrer" className="cal-btn cal-btn--blue" style={{ padding: '12px 20px', fontSize: 14, textDecoration: 'none' }}>
                Open in new tab {Icon.arrowR({ size: 13, color: '#fff' })}
              </a>
              <button className="cal-btn cal-btn--ghost" style={{
                padding: '12px 20px', fontSize: 14,
                background: dark ? 'rgba(255,255,255,0.05)' : '#fff',
                color: dark ? '#fff' : 'var(--cal-ink)',
                border: `1px solid ${dark ? 'rgba(255,255,255,0.18)' : 'var(--cal-line)'}`,
              }}>
                Book a demo call
              </button>
              {showBadges && (
                <div style={{
                  display: 'inline-flex', alignItems: 'center', gap: 14, marginLeft: 8,
                  fontSize: 12.5, color: dark ? 'rgba(255,255,255,0.6)' : 'var(--cal-muted)',
                }}>
                  <span>⌘K search</span>
                  <span style={{ opacity: 0.4 }}>·</span>
                  <span>Harbour Talent pre-loaded</span>
                  <span style={{ opacity: 0.4 }}>·</span>
                  <span>Reset anytime</span>
                </div>
              )}
            </div>
          </div>
        </div>
        {/* The product, big */}
        <div style={{ maxWidth: 1280, margin: '0 auto' }}>{Frame}</div>
      </div>
    </section>
  );
}

Object.assign(window, { LiveDemo, BrowserChrome, DEMO_URL });
