// Reusable portrait video highlight: muted autoplay + loop + a sound toggle.
// Defaults to sound OFF and auto-playing (required for browser autoplay);
// the speaker button unmutes. Used for the Bulk Shoot reel and the
// model-swap demo. `flip` puts the video on the left, text on the right.
function PSVideoHighlight({ id, eyebrow, headline, body, src }) {
  const ref = React.useRef(null);
  const [muted, setMuted] = React.useState(true);

  // Lazy: don't download the video until it scrolls into view, then play it
  // (and pause when it leaves). Keeps first paint light, big LCP win.
  React.useEffect(() => {
    const v = ref.current;
    if (!v) return;
    const io = new IntersectionObserver((entries) => {
      entries.forEach((e) => {
        if (e.isIntersecting) v.play().catch(() => {});
        else v.pause();
      });
    }, { threshold: 0.2 });
    io.observe(v);
    return () => io.disconnect();
  }, []);

  const toggleSound = () => {
    const v = ref.current;
    if (!v) return;
    const next = !v.muted;
    v.muted = next;
    setMuted(next);
    if (!next) { v.play().catch(() => {}); }
  };

  const flip = id === 'model-swap';

  const text = (
    <div key="t" className="ps-vh-text" style={{ display: 'flex', flexDirection: 'column', justifyContent: 'center' }}>
      <div className="eyebrow" style={{ marginBottom: 14 }}>
        <span style={{ display: 'inline-block', width: 24, height: 1, background: 'var(--cocoa)', verticalAlign: 'middle', marginRight: 12 }}></span>
        {eyebrow}
      </div>
      <h2 className="celestial" style={{ fontSize: 'clamp(40px, 5vw, 80px)', color: 'var(--cocoa)', margin: 0, lineHeight: 0.98 }}>{headline}</h2>
      <p style={{ fontFamily: '"La Villa", sans-serif', fontSize: 17, lineHeight: 1.6, color: 'var(--fg-2)', maxWidth: 440, marginTop: 24 }}>{body}</p>
    </div>
  );

  const video = (
    <div key="v" className="ps-vh-video" style={{ display: 'flex', justifyContent: flip ? 'flex-start' : 'flex-end' }}>
      <div style={{ position: 'relative', width: 'min(100%, 360px)', aspectRatio: '9 / 16', overflow: 'hidden', borderRadius: 6, background: 'var(--cocoa-deep, #1d1814)', boxShadow: '0 30px 70px -24px rgba(42,36,32,0.5)' }}>
        <video ref={ref} src={src} muted loop playsInline preload="none"
          style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'cover' }} />
        <button onClick={toggleSound} aria-label={muted ? 'Turn sound on' : 'Mute'} style={{
          position: 'absolute', bottom: 14, right: 14, width: 42, height: 42, borderRadius: 999,
          border: '1px solid rgba(242,236,228,0.25)', cursor: 'pointer',
          background: 'rgba(42,36,32,0.5)', backdropFilter: 'blur(8px)', WebkitBackdropFilter: 'blur(8px)',
          color: 'var(--cream)', display: 'flex', alignItems: 'center', justifyContent: 'center',
        }}>
          {muted ? (
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M11 5 6 9H2v6h4l5 4V5z"/><line x1="23" y1="9" x2="17" y2="15"/><line x1="17" y1="9" x2="23" y2="15"/></svg>
          ) : (
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M11 5 6 9H2v6h4l5 4V5z"/><path d="M15.5 8.5a5 5 0 0 1 0 7"/><path d="M19 5a9 9 0 0 1 0 14"/></svg>
          )}
        </button>
      </div>
    </div>
  );

  return (
    <section id={id} className="ps-section" data-mobile-pad="true" data-mobile-y="lg" style={{ background: 'var(--cream)', padding: '120px 48px' }}>
      <div className="ps-stack-mobile" style={{ maxWidth: 1320, margin: '0 auto', display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 64, alignItems: 'center' }}>
        {flip ? [video, text] : [text, video]}
      </div>
    </section>
  );
}

Object.assign(window, { PSVideoHighlight });
