/* global React */
const { useState, useEffect, useRef, useCallback, useMemo } = React;

// ============================================================
// LOGO
// ============================================================

function NeroLogo({ size = 22 }) {
  return (
    <div className="nav__logo">
      <NeroMark size={size} />
      <span>NeroView</span>
    </div>
  );
}

function NeroMark({ size = 22 }) {
  // simple "viewfinder" mark — circle inside ring, accent color
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <rect x="1.5" y="1.5" width="21" height="21" rx="4" stroke="currentColor" strokeOpacity="0" fill="var(--accent)" />
      <circle cx="12" cy="12" r="6.5" fill="var(--bg)" />
      <circle cx="12" cy="12" r="3.2" fill="var(--accent)" />
      <line x1="12" y1="1" x2="12" y2="4" stroke="var(--bg)" strokeWidth="1.5" />
      <line x1="12" y1="20" x2="12" y2="23" stroke="var(--bg)" strokeWidth="1.5" />
      <line x1="1" y1="12" x2="4" y2="12" stroke="var(--bg)" strokeWidth="1.5" />
      <line x1="20" y1="12" x2="23" y2="12" stroke="var(--bg)" strokeWidth="1.5" />
    </svg>
  );
}

// ============================================================
// COMPANY LOGO PLACEHOLDERS
// ============================================================

function CompanyLogo({ name, mark }) {
  return (
    <div className="proofbar__logo">
      {mark}
      <span>{name}</span>
    </div>
  );
}

const LogoMarks = {
  square:   <svg width="20" height="20" viewBox="0 0 20 20"><rect x="2" y="2" width="16" height="16" rx="2" fill="currentColor" /></svg>,
  triangle: <svg width="20" height="20" viewBox="0 0 20 20"><path d="M10 2 L18 18 L2 18 Z" fill="currentColor" /></svg>,
  circle:   <svg width="20" height="20" viewBox="0 0 20 20"><circle cx="10" cy="10" r="8" fill="currentColor" /></svg>,
  bars:     <svg width="22" height="20" viewBox="0 0 22 20"><rect x="0" y="6" width="4" height="14" fill="currentColor" /><rect x="6" y="2" width="4" height="18" fill="currentColor" /><rect x="12" y="10" width="4" height="10" fill="currentColor" /><rect x="18" y="6" width="4" height="14" fill="currentColor" /></svg>,
  rings:    <svg width="24" height="20" viewBox="0 0 24 20"><circle cx="7" cy="10" r="6" stroke="currentColor" strokeWidth="2" fill="none" /><circle cx="16" cy="10" r="6" stroke="currentColor" strokeWidth="2" fill="none" /></svg>,
  dot:      <svg width="22" height="20" viewBox="0 0 22 20"><circle cx="11" cy="10" r="4" fill="currentColor" /><circle cx="11" cy="10" r="9" stroke="currentColor" strokeWidth="1.5" fill="none" /></svg>,
  slash:    <svg width="24" height="20" viewBox="0 0 24 20"><path d="M3 17 L21 3" stroke="currentColor" strokeWidth="3" strokeLinecap="round" /></svg>,
};

// ============================================================
// TAG / PILL
// ============================================================

function Tag({ children, accent, dot }) {
  let cls = "tag";
  if (accent) cls += " tag--accent";
  if (dot) cls += " tag--dot";
  return <span className={cls}>{children}</span>;
}

// ============================================================
// TRANSCRIPT SNIPPET
// ============================================================

function Transcript({ time, speaker, children }) {
  return (
    <div className="transcript">
      <div className="card__label">
        <span className="card__label-dot"></span>
        TRANSCRIPT · {speaker || "P-042"}
      </div>
      <div>
        <span className="transcript__time">{time}</span>
        {children}
      </div>
    </div>
  );
}

// ============================================================
// EMOTION / ATTENTION TIMELINE
// ============================================================

function EmotionTimeline({ height = 56, moments = [], data }) {
  // generate a sample wavy data path
  const points = useMemo(() => {
    if (data) return data;
    const N = 56;
    return Array.from({ length: N }, (_, i) => {
      const x = i / (N - 1);
      const noise = Math.sin(i * 0.6) * 0.18 + Math.sin(i * 0.23) * 0.32 + Math.sin(i * 1.1) * 0.1;
      return 0.5 + noise;
    });
  }, [data]);

  const path = useMemo(() => {
    const w = 100, h = 100;
    return points.map((p, i) => {
      const x = (i / (points.length - 1)) * w;
      const y = (1 - p) * h;
      return `${i === 0 ? "M" : "L"} ${x.toFixed(2)} ${y.toFixed(2)}`;
    }).join(" ");
  }, [points]);

  return (
    <div>
      <div className="card__label">
        <span className="card__label-dot"></span>
        EMOTION TIMELINE · 0:00 – 4:32
      </div>
      <div className="timeline" style={{ height }}>
        <div className="timeline__bg" />
        <svg className="timeline__line" viewBox="0 0 100 100" preserveAspectRatio="none" style={{ width: "100%", height: "100%" }}>
          <path d={path} stroke="var(--fg-muted)" strokeWidth="1.2" fill="none" vectorEffect="non-scaling-stroke" />
          <path d={path + " L 100 100 L 0 100 Z"} fill="url(#tlGrad)" opacity="0.35" />
          <defs>
            <linearGradient id="tlGrad" x1="0" y1="0" x2="0" y2="1">
              <stop offset="0%" stopColor="var(--accent)" stopOpacity="0.6" />
              <stop offset="100%" stopColor="var(--accent)" stopOpacity="0" />
            </linearGradient>
          </defs>
        </svg>
        <div className="timeline__moments">
          {moments.map((m, i) => (
            <div key={i} className="timeline__moment" style={{ left: `${m * 100}%` }} />
          ))}
        </div>
      </div>
      <div className="timeline__axis">
        <span>0:00</span><span>1:30</span><span>3:00</span><span>4:32</span>
      </div>
    </div>
  );
}

// ============================================================
// CONFIDENCE BAR
// ============================================================

function Confidence({ pct = 0.86, label = "CONFIDENCE" }) {
  return (
    <div className="confidence">
      <span>{label}</span>
      <div className="confidence__bar"><i style={{ transform: `scaleX(${pct})` }} /></div>
      <span>{Math.round(pct * 100)}%</span>
    </div>
  );
}

// ============================================================
// INSIGHT CARD
// ============================================================

function InsightCard({ title, segment, clips, participants, segments, confidence, accent, children }) {
  return (
    <div className={`card ${accent ? "card--accent" : ""}`}>
      <div className="card__label">
        <span className="card__label-dot"></span>
        CLUSTER · INSIGHT
      </div>
      <h4 className="insight__title">{title}</h4>
      {children}
      <div className="insight__meta">
        <span><i className="insight__bullet" />{clips} clips</span>
        <span><i className="insight__bullet" />{participants} participants</span>
        <span><i className="insight__bullet" />{segments} segments</span>
      </div>
      {confidence !== undefined && <Confidence pct={confidence} />}
    </div>
  );
}

// ============================================================
// VIDEO TILE
// ============================================================

function VideoTile({ participant = "P-042", duration = "04:32" }) {
  return (
    <div className="videotile">
      <div className="videotile__bg" />
      <div className="videotile__person" />
      <div className="videotile__chip"><span className="videotile__chip-dot" />REC · {participant}</div>
      <div className="videotile__waveform">
        {Array.from({ length: 22 }).map((_, i) => (
          <span key={i} style={{ height: `${4 + Math.abs(Math.sin(i * 0.7)) * 10}px`, opacity: 0.4 + Math.abs(Math.sin(i * 0.4)) * 0.6 }} />
        ))}
      </div>
      <div className="videotile__timestamp">{duration}</div>
    </div>
  );
}

// ============================================================
// expose globally
// ============================================================

Object.assign(window, {
  NeroLogo, NeroMark, CompanyLogo, LogoMarks,
  Tag, Transcript, EmotionTimeline, Confidence, InsightCard, VideoTile,
});
