/* global React */

const DemoPlayer = () => {
  const [playing, setPlaying]         = React.useState(false);
  const [progress, setProgress]       = React.useState(0);
  const [muted, setMuted]             = React.useState(true);
  const [currentTime, setCurrentTime] = React.useState(0);
  const [duration, setDuration]       = React.useState(0);
  const [hovering, setHovering]       = React.useState(false);
  const videoRef = React.useRef(null);

  const fmtTime = (s) => {
    const m = Math.floor(s / 60);
    return `${m}:${Math.floor(s % 60).toString().padStart(2, "0")}`;
  };

  const toggle = () => {
    const v = videoRef.current;
    if (!v) return;
    if (playing) { v.pause(); setPlaying(false); }
    else         { v.play();  setPlaying(true);  }
  };

  const onTimeUpdate = () => {
    const v = videoRef.current;
    if (!v) return;
    setCurrentTime(v.currentTime);
    setProgress(v.duration ? (v.currentTime / v.duration) * 100 : 0);
  };

  const seek = (e) => {
    const v = videoRef.current;
    if (!v || !v.duration) return;
    const rect = e.currentTarget.getBoundingClientRect();
    v.currentTime = ((e.clientX - rect.left) / rect.width) * v.duration;
  };

  const toggleMute = (e) => {
    e.stopPropagation();
    const v = videoRef.current;
    if (!v) return;
    v.muted = !v.muted;
    setMuted(v.muted);
  };

  const goFullscreen = (e) => {
    e.stopPropagation();
    const v = videoRef.current;
    if (!v) return;
    (v.requestFullscreen || v.webkitRequestFullscreen || (() => {})).call(v);
  };

  return (
    <div id="watch-demo" style={{
      padding: "96px 32px 112px",
      borderTop: "1px solid var(--line)",
      background: "var(--bg)"
    }}>
      <div style={{ maxWidth: 1100, margin: "0 auto" }}>

        {/* Section header */}
        <div className="snap-in" style={{ marginBottom: 44 }}>

          {/* Mode badge row */}
          <div style={{ display: "flex", alignItems: "center", gap: 12, marginBottom: 20, flexWrap: "wrap" }}>
            <span className="tag" style={{ color: "var(--fg-2)", borderColor: "var(--line-2)" }}>
              <span className="dot pulse"></span>PPC AGENT · ADS CSV ANALYSIS
            </span>
            <span className="tag" style={{
              color: "#2563eb", borderColor: "#2563eb",
              background: "var(--bg-1)"
            }}>
              L1 · ADVISOR MODE
            </span>
          </div>

          <h2 className="display" style={{
            fontSize: 56, lineHeight: 1.0, letterSpacing: "-0.03em",
            margin: "0 0 12px", fontWeight: 700
          }}>
            You see the data.<br />You make the call.
          </h2>

          <p style={{ fontSize: 16, color: "var(--fg-2)", lineHeight: 1.6, maxWidth: 600, margin: 0 }}>
            This is <strong style={{ color: "var(--fg-1)" }}>Advisor</strong> — the entry altitude.
            HEXA-V reads your ads CSV, runs PPC analysis, and surfaces exactly what needs attention:
            overbid keywords, wasted spend, harvest opportunities.
            No automation. Every decision stays with you.
            The agent tells you what to do — you decide whether to do it.
          </p>

          {/* Advisor vs Co-Pilot callout */}
          <div style={{
            marginTop: 28, display: "grid", gridTemplateColumns: "1fr 1fr",
            gap: 0, border: "1px solid var(--line)", maxWidth: 600
          }}>
            {[
              {
                tier: "L1 · ADVISOR", subtitle: "This demo",
                desc: "Reads your CSV. Tells you what's wrong. You act on it.",
                active: true
              },
              {
                tier: "L2 · CO-PILOT", subtitle: "Next tier",
                desc: "Same analysis, but actions queue for your one-click approval.",
                active: false
              }
            ].map((t, i) => (
              <div key={i} style={{
                padding: "18px 20px",
                borderRight: i === 0 ? "1px solid var(--line)" : "none",
                background: t.active ? "var(--bg-1)" : "transparent",
                position: "relative"
              }}>
                {t.active && <div style={{
                  position: "absolute", top: 0, left: 0, right: 0, height: 2,
                  background: "var(--accent)"
                }} />}
                <div className="mono" style={{
                  fontSize: 10, letterSpacing: "0.18em",
                  color: t.active ? "var(--accent)" : "var(--fg-3)"
                }}>{t.tier}</div>
                <div className="mono" style={{
                  fontSize: 9, letterSpacing: "0.12em", color: "var(--fg-3)",
                  marginTop: 2, marginBottom: 8
                }}>{t.subtitle}</div>
                <div style={{ fontSize: 13, color: t.active ? "var(--fg-1)" : "var(--fg-2)", lineHeight: 1.5 }}>
                  {t.desc}
                </div>
              </div>
            ))}
          </div>

          {/* Synthetic data disclaimer */}
          <div style={{
            marginTop: 16, display: "inline-flex", alignItems: "baseline", gap: 10,
            padding: "7px 14px", border: "1px solid var(--line)",
            background: "var(--bg-1)"
          }}>
            <span className="mono" style={{ fontSize: 10, color: "var(--accent)", letterSpacing: "0.18em" }}>⚠ NOTE</span>
            <span className="mono" style={{ fontSize: 11, color: "var(--fg-3)", letterSpacing: "0.06em" }}>
              Synthetic data only — not real account figures. Numbers are illustrative.
            </span>
          </div>

        </div>

        {/* ── Player shell ── */}
        <div className="snap-in" style={{
          position: "relative",
          border: "1px solid var(--line-2)",
          boxShadow: [
            "0 0 0 1px var(--line)",
            "0 0 48px rgba(37,99,235,0.3)",
            "0 0 96px rgba(37,99,235,0.12)",
            "0 32px 64px rgba(0,0,0,0.5)"
          ].join(", "),
          background: "#000",
          overflow: "hidden",
          userSelect: "none"
        }}>

          {/* Accent corner brackets */}
          {["tl","tr","bl","br"].map(c => (
            <div key={c} className={`card-corner ${c}`} style={{
              width: 18, height: 18, borderWidth: 2,
              borderColor: "var(--accent)"
            }} />
          ))}

          {/* ── Window bar ── */}
          <div style={{
            display: "flex", alignItems: "center", justifyContent: "space-between",
            padding: "9px 18px", background: "var(--bg-1)",
            borderBottom: "1px solid var(--line)",
            fontFamily: "var(--font-mono)", fontSize: 11
          }}>
            <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
              <span style={{ width: 9, height: 9, borderRadius: "50%", background: "#ef4444", display:"inline-block" }} />
              <span style={{ width: 9, height: 9, borderRadius: "50%", background: "#f59e0b", display:"inline-block" }} />
              <span style={{ width: 9, height: 9, borderRadius: "50%", background: "#22c55e", display:"inline-block" }} />
              <span style={{ marginLeft: 14, color: "var(--fg-2)", letterSpacing: "0.1em" }}>
                HEXA-V · PPC.AGENT · ADS.CSV.ANALYSIS
              </span>
            </div>
            <div style={{ display: "flex", gap: 20, alignItems: "center" }}>
              <span className="pulse" style={{ color: "var(--accent)", letterSpacing: "0.12em" }}>● REC</span>
              <span style={{ color: "var(--fg-3)" }}>2026.05.15</span>
            </div>
          </div>

          {/* ── Video + overlays ── */}
          <div
            style={{ position: "relative", cursor: "pointer", lineHeight: 0 }}
            onClick={toggle}
            onMouseEnter={() => setHovering(true)}
            onMouseLeave={() => setHovering(false)}
          >
            <video
              ref={videoRef}
              src="demo.mp4"
              loop
              muted
              playsInline
              style={{ width: "100%", display: "block", maxHeight: 560, objectFit: "contain", background: "#000" }}
              onTimeUpdate={onTimeUpdate}
              onLoadedMetadata={() => setDuration(videoRef.current?.duration || 0)}
              onPlay={() => setPlaying(true)}
              onPause={() => setPlaying(false)}
            />

            {/* Scanline overlay */}
            <div style={{
              position: "absolute", inset: 0, pointerEvents: "none",
              backgroundImage: "repeating-linear-gradient(to bottom, transparent 0, transparent 3px, rgba(0,0,0,0.14) 3px, rgba(0,0,0,0.14) 4px)"
            }} />

            {/* Inner vignette */}
            <div style={{
              position: "absolute", inset: 0, pointerEvents: "none",
              boxShadow: "inset 0 0 80px rgba(0,0,0,0.55)"
            }} />

            {/* Accent glow edge */}
            <div style={{
              position: "absolute", inset: 0, pointerEvents: "none",
              boxShadow: "inset 0 0 0 1px rgba(37,99,235,0.2)"
            }} />

            {/* LIVE DEMO badge */}
            <div style={{
              position: "absolute", top: 16, right: 16,
              fontFamily: "var(--font-mono)", fontSize: 10, letterSpacing: "0.18em",
              padding: "5px 12px", background: "rgba(0,0,0,0.8)",
              border: "1px solid var(--accent)", color: "var(--accent)",
              display: "flex", alignItems: "center", gap: 7
            }}>
              <span style={{
                width: 5, height: 5, borderRadius: "50%",
                background: "var(--accent)", display: "inline-block",
                animation: "pulseDot 1.8s infinite"
              }} />
              LIVE DEMO
            </div>

            {/* Current time stamp */}
            <div style={{
              position: "absolute", bottom: 16, left: 16,
              fontFamily: "var(--font-mono)", fontSize: 10, letterSpacing: "0.1em",
              color: "rgba(255,255,255,0.65)", background: "rgba(0,0,0,0.55)",
              padding: "3px 9px", pointerEvents: "none"
            }}>
              {fmtTime(currentTime)} / {fmtTime(duration)}
            </div>

            {/* Play overlay — shown when paused or on hover */}
            <div style={{
              position: "absolute", inset: 0,
              display: "flex", alignItems: "center", justifyContent: "center",
              background: playing && !hovering ? "transparent" : "rgba(0,0,0,0.32)",
              transition: "background 200ms ease",
              pointerEvents: "none"
            }}>
              <div style={{
                width: 76, height: 76,
                border: "2px solid rgba(255,255,255,0.85)",
                borderRadius: "50%",
                display: "flex", alignItems: "center", justifyContent: "center",
                background: "rgba(0,0,0,0.45)",
                backdropFilter: "blur(6px)",
                opacity: playing && !hovering ? 0 : 1,
                transform: playing && !hovering ? "scale(0.8)" : "scale(1)",
                transition: "opacity 200ms var(--spring), transform 200ms var(--spring)"
              }}>
                <span style={{ fontSize: 26, marginLeft: 5, color: "#fff" }}>
                  {playing ? "⏸" : "▶"}
                </span>
              </div>
            </div>
          </div>

          {/* ── Custom controls ── */}
          <div style={{
            background: "var(--bg-1)",
            borderTop: "1px solid var(--line)",
            padding: "10px 18px 12px",
            display: "flex", flexDirection: "column", gap: 9
          }}>

            {/* Progress bar */}
            <div
              onClick={seek}
              style={{
                height: 4, background: "var(--bg-3)",
                cursor: "pointer", position: "relative"
              }}
            >
              {/* Filled */}
              <div style={{
                position: "absolute", left: 0, top: 0, height: "100%",
                width: `${progress}%`,
                background: "var(--accent)",
                boxShadow: "0 0 8px var(--accent-glow)",
                transition: "width 0.1s linear"
              }} />
              {/* Thumb */}
              <div style={{
                position: "absolute", top: "50%", left: `${progress}%`,
                width: 12, height: 12,
                background: "var(--accent)",
                borderRadius: "50%",
                transform: "translate(-50%, -50%)",
                boxShadow: "0 0 10px var(--accent)"
              }} />
            </div>

            {/* Button row */}
            <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
              <div style={{ display: "flex", alignItems: "center", gap: 10 }}>

                <button onClick={toggle} style={{
                  background: "transparent",
                  border: "1px solid var(--line-2)",
                  color: "var(--fg-1)",
                  fontFamily: "var(--font-mono)", fontSize: 11,
                  padding: "5px 14px", cursor: "pointer",
                  letterSpacing: "0.1em", lineHeight: 1,
                  transition: "border-color 150ms, color 150ms"
                }}>
                  {playing ? "⏸ PAUSE" : "▶ PLAY"}
                </button>

                <button onClick={toggleMute} style={{
                  background: "transparent",
                  border: "1px solid var(--line-2)",
                  color: muted ? "var(--fg-3)" : "var(--fg-1)",
                  fontFamily: "var(--font-mono)", fontSize: 11,
                  padding: "5px 12px", cursor: "pointer",
                  letterSpacing: "0.1em", lineHeight: 1,
                  transition: "border-color 150ms, color 150ms"
                }}>
                  {muted ? "▷ UNMUTE" : "♬ AUDIO"}
                </button>

                <span className="mono" style={{ fontSize: 11, color: "var(--fg-3)", letterSpacing: "0.1em" }}>
                  {fmtTime(currentTime)} / {fmtTime(duration)}
                </span>
              </div>

              <div style={{ display: "flex", gap: 10, alignItems: "center" }}>
                <span className="mono" style={{ fontSize: 10, color: "var(--fg-3)", letterSpacing: "0.14em" }}>
                  ↻ LOOP ON
                </span>
                <button onClick={goFullscreen} style={{
                  background: "transparent",
                  border: "1px solid var(--line-2)",
                  color: "var(--fg-1)",
                  fontFamily: "var(--font-mono)", fontSize: 11,
                  padding: "5px 12px", cursor: "pointer",
                  letterSpacing: "0.1em", lineHeight: 1
                }}>
                  ⛶ FULL
                </button>
              </div>
            </div>
          </div>
        </div>

        {/* CTA strip below player */}
        <div style={{ marginTop: 32, display: "flex", gap: 16, alignItems: "center" }}>
          <a
            href={(window.HEXA || {}).SIGNUP_URL || "#"}
            className="btn btn-primary"
            style={{ textDecoration: "none" }}
          >
            Deploy in 6 minutes →
          </a>
          <span className="mono" style={{ fontSize: 11, color: "var(--fg-3)", letterSpacing: "0.14em" }}>
            // 14-day pilot · no card required
          </span>
        </div>

      </div>
    </div>
  );
};

window.DemoPlayer = DemoPlayer;
