// Shared visual bits for the website kit — placeholder video, section wrapper

// Free sample clip used as a placeholder throughout the demo.
window.NA_PLACEHOLDER_VIDEO = "https://media.w3.org/2010/05/sintel/trailer.mp4";

function VideoTile({ label, tc = "00:00:24:00", ratio = "16 / 9", rush = false, autoplay = false, src }) {
  const ref = React.useRef(null);
  const [playing, setPlaying] = React.useState(autoplay);
  const [muted, setMuted] = React.useState(true);

  // Keep the underlying media element's muted property in sync with state.
  React.useEffect(() => { if (ref.current) ref.current.muted = muted; }, [muted]);

  const toggle = () => {
    const v = ref.current; if (!v) return;
    if (v.paused) { v.play(); setPlaying(true); } else { v.pause(); setPlaying(false); }
  };
  const toggleSound = (e) => {
    e.stopPropagation();
    const v = ref.current; if (!v) return;
    const nextMuted = !muted;
    v.muted = nextMuted;
    setMuted(nextMuted);
    // Turning sound on should also make sure the clip is actually playing.
    if (!nextMuted && v.paused) { v.play(); setPlaying(true); }
  };
  return (
    <figure
      onClick={toggle}
      style={{
        margin: 0, position: "relative", borderRadius: ratio === "auto" ? 0 : "var(--r-md)",
        aspectRatio: ratio === "auto" ? undefined : ratio, height: ratio === "auto" ? "100%" : undefined,
        overflow: "hidden", background: "linear-gradient(135deg, #16171d, #24262f)", border: ratio === "auto" ? "none" : "1px solid var(--border-soft)", cursor: "pointer",
      }}>
      <video
        ref={ref} src={src || window.NA_PLACEHOLDER_VIDEO} muted={muted} loop playsInline
        autoPlay={autoplay} preload="metadata"
        style={{ width: "100%", height: "100%", objectFit: "cover", display: "block", filter: "saturate(0.92) contrast(1.02)" }}
      />
      {/* subtle top gradient for legibility */}
      <div style={{ position: "absolute", inset: 0, background: "linear-gradient(180deg, rgba(10,11,13,0.35), transparent 30%, transparent 60%, rgba(10,11,13,0.55))", pointerEvents: "none" }} />
      {!playing && (
        <div style={{ position: "absolute", inset: 0, display: "flex", alignItems: "center", justifyContent: "center", pointerEvents: "none" }}>
          <span style={{
            width: 56, height: 56, borderRadius: "50%", display: "flex", alignItems: "center", justifyContent: "center",
            background: "rgba(10,11,13,0.4)", color: "#fff", border: "1.5px solid rgba(255,255,255,0.7)", backdropFilter: "blur(4px)",
          }}><i data-lucide="play" style={{ marginLeft: 3, width: 20, height: 20 }}></i></span>
        </div>
      )}
      {/* Sound on/off toggle — autoplay stays muted (browsers require it); click to hear audio. */}
      <button
        onClick={toggleSound}
        aria-label={muted ? "Turn sound on" : "Turn sound off"}
        title={muted ? "Sound off — click for audio" : "Sound on"}
        style={{
          position: "absolute", top: 14, right: 14, zIndex: 3,
          width: 34, height: 34, padding: 0, borderRadius: "var(--r-xs)",
          display: "flex", alignItems: "center", justifyContent: "center",
          background: "rgba(10,11,13,0.5)", color: "var(--paper-100)",
          border: "1px solid rgba(244,242,234,0.32)", backdropFilter: "blur(4px)", cursor: "pointer",
        }}>
        {muted ? (
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
            <polygon points="11 5 6 9 2 9 2 15 6 15 11 19 11 5" />
            <line x1="23" y1="9" x2="17" y2="15" />
            <line x1="17" y1="9" x2="23" y2="15" />
          </svg>
        ) : (
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
            <polygon points="11 5 6 9 2 9 2 15 6 15 11 19 11 5" />
            <path d="M15.54 8.46a5 5 0 0 1 0 7.07" />
            <path d="M19.07 4.93a10 10 0 0 1 0 14.14" />
          </svg>
        )}
      </button>
      {rush && (
        <span style={{
          position: "absolute", top: 14, left: 14, fontFamily: "var(--font-mono)", fontSize: 10.5,
          letterSpacing: "0.12em", color: "var(--paper-100)", border: "1px solid rgba(244,242,234,0.4)",
          padding: "3px 8px", borderRadius: 2, textTransform: "uppercase", backdropFilter: "blur(4px)",
        }}>Rush delivery</span>
      )}
      {label && (
        <figcaption style={{
          position: "absolute", left: 16, right: 16, bottom: 14, display: "flex",
          alignItems: "center", justifyContent: "space-between", pointerEvents: "none",
        }}>
          <span style={{ fontFamily: "var(--font-sans)", fontWeight: 600, color: "var(--paper-100)", fontSize: 14 }}>{label}</span>
          <span style={{ fontFamily: "var(--font-mono)", fontSize: 11.5, color: "var(--paper-100)", opacity: 0.85, letterSpacing: "0.04em" }}>{tc}</span>
        </figcaption>
      )}
    </figure>
  );
}

function Section({ id, children, style = {} }) {
  return (
    <section data-screen-label={id} style={{ padding: "var(--section-y) var(--gutter)", ...style }}>
      <div style={{ maxWidth: "var(--container)", margin: "0 auto" }}>{children}</div>
    </section>
  );
}

// Back-compat alias
window.VideoThumb = VideoTile;
window.VideoTile = VideoTile;
window.Section = Section;
