// Two preview card components for the hero stack.
// SESPreview: light card with WhatsApp conversation (teal corner glow).
// VoicePreview: dark card with LIVE caller, waveform (accent-teal), intent/status row.

const { useEffect, useState, useRef } = React;

// ---------- shared waveform ----------
function Waveform({ bars = 28, color = "var(--accent-teal)", height = 36 }) {
  // deterministic but varied heights
  const heights = React.useMemo(() => {
    const arr = [];
    for (let i = 0; i < bars; i++) {
      // bell-ish curve, jittered
      const t = i / (bars - 1);
      const bell = Math.sin(t * Math.PI);
      const noise = ((Math.sin(i * 7.3) + 1) / 2) * 0.5 + 0.3;
      arr.push(0.25 + bell * noise * 0.85);
    }
    return arr;
  }, [bars]);

  return (
    <div style={{ display: "flex", alignItems: "center", gap: 3, height }}>
      {heights.map((h, i) => (
        <div
          key={i}
          className="wave-bar"
          style={{
            width: 3,
            height: `${h * height}px`,
            background: color,
            borderRadius: 2,
            animationDelay: `${(i % 8) * 0.07}s`
          }}
        />
      ))}
    </div>
  );
}

// ---------- SES (WhatsApp) preview ----------
function SESPreview({ t }) {
  const p = t.preview.ses;
  return (
    <div style={{
      position: "relative",
      height: "100%",
      width: "100%",
      background: "var(--bg-white)",
      borderRadius: 22,
      overflow: "hidden",
      boxShadow: "0 30px 60px -25px rgba(11,15,20,0.25), 0 12px 24px -16px rgba(11,15,20,0.12)",
      border: "1px solid var(--border-light)",
      display: "flex",
      flexDirection: "column"
    }}>
      <div className="glow-teal" />

      {/* header */}
      <div style={{
        position: "relative",
        zIndex: 2,
        display: "flex",
        alignItems: "center",
        gap: 10,
        padding: "16px 18px",
        borderBottom: "1px solid var(--border-light)",
        background: "rgba(255,255,255,0.6)"
      }}>
        <Icon.WhatsApp size={22} />
        <div style={{ flex: 1, minWidth: 0 }}>
          <div className="mono" style={{ fontSize: 10, letterSpacing: "0.12em", color: "var(--muted-ink)", textTransform: "uppercase" }}>
            {p.chip}
          </div>
          <div style={{ fontSize: 13, fontWeight: 600, marginTop: 1 }}>Dr.ssa Bianchi · Studio</div>
        </div>
        <div className="mono" style={{ fontSize: 11, color: "var(--muted-ink)" }}>{p.ago}</div>
      </div>

      {/* conversation */}
      <div style={{
        position: "relative",
        zIndex: 2,
        flex: 1,
        padding: "20px 18px 14px",
        display: "flex",
        flexDirection: "column",
        gap: 10,
        background: "linear-gradient(180deg, rgba(155,201,210,0.06) 0%, transparent 40%)"
      }}>
        {/* incoming */}
        <div style={{
          alignSelf: "flex-start",
          maxWidth: "85%",
          background: "var(--bg-cream)",
          padding: "10px 12px 12px",
          borderRadius: "14px 14px 14px 4px",
          fontSize: 13.5,
          lineHeight: 1.45,
          color: "var(--fg-ink)",
          border: "1px solid var(--border-light)"
        }}>
          {p.bot[0].text}
          <div style={{ display: "flex", gap: 6, marginTop: 10, flexWrap: "wrap" }}>
            <span style={{ fontSize: 12, fontWeight: 500, padding: "5px 10px", border: "1px solid var(--cat-green)", color: "var(--cat-green)", borderRadius: 999 }}>{p.replyConfirm}</span>
            <span style={{ fontSize: 12, fontWeight: 500, padding: "5px 10px", border: "1px solid var(--border-light)", color: "var(--muted-ink)", borderRadius: 999 }}>{p.replyReschedule}</span>
          </div>
        </div>

        {/* outgoing reply */}
        <div style={{
          alignSelf: "flex-end",
          maxWidth: "75%",
          background: "#DCF8C6",
          color: "#1a2a16",
          padding: "8px 12px 9px",
          borderRadius: "14px 14px 4px 14px",
          fontSize: 13.5,
          fontWeight: 500
        }}>
          {p.userReply.text}
          <div className="mono" style={{ fontSize: 10, opacity: 0.6, marginTop: 2, textAlign: "right" }}>{p.userReply.time}  ✓✓</div>
        </div>

        {/* divider with follow-up label */}
        <div className="mono" style={{
          alignSelf: "center", fontSize: 9, letterSpacing: "0.18em", color: "var(--muted-ink)",
          marginTop: 4, marginBottom: 2
        }}>
          {p.followUpTitle}
        </div>

        {/* follow-up */}
        <div style={{
          alignSelf: "flex-start",
          maxWidth: "88%",
          background: "var(--bg-cream)",
          padding: "10px 12px 11px",
          borderRadius: "14px 14px 14px 4px",
          fontSize: 13,
          lineHeight: 1.45,
          color: "var(--fg-ink)",
          border: "1px solid var(--border-light)"
        }}>
          {p.followUp.text}
        </div>
      </div>

      {/* result strip */}
      <div style={{
        position: "relative",
        zIndex: 2,
        margin: "0 14px 14px",
        padding: "12px 14px",
        background: "var(--soft-emerald-bg)",
        border: "1px solid rgba(4, 120, 87, 0.15)",
        borderRadius: 14,
        display: "flex",
        alignItems: "center",
        gap: 12,
        justifyContent: "space-between"
      }}>
        <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
          <div style={{
            width: 26, height: 26, borderRadius: 8,
            background: "var(--soft-emerald-fg)", color: "white",
            display: "grid", placeItems: "center"
          }}>
            <Icon.Check size={14} />
          </div>
          <div className="display" style={{ fontSize: 14, color: "var(--soft-emerald-fg)" }}>{p.result.headline}</div>
        </div>
        <div style={{ display: "flex", alignItems: "baseline", gap: 8 }}>
          <span className="display" style={{ fontSize: 16, color: "var(--soft-emerald-fg)" }}>{p.result.revenue}</span>
          <span className="mono" style={{ fontSize: 10, color: "var(--soft-emerald-fg)", opacity: 0.7 }}>{p.result.time}</span>
        </div>
      </div>
    </div>
  );
}

// ---------- Voice Agent preview ----------
function VoicePreview({ t }) {
  const p = t.preview.voice;
  const [tick, setTick] = useState(42);
  useEffect(() => {
    const id = setInterval(() => setTick(v => v + 1), 1000);
    return () => clearInterval(id);
  }, []);
  const mm = String(Math.floor(tick / 60)).padStart(2, "0");
  const ss = String(tick % 60).padStart(2, "0");

  return (
    <div style={{
      position: "relative",
      height: "100%",
      width: "100%",
      background: "var(--bg-ink)",
      borderRadius: 22,
      overflow: "hidden",
      boxShadow: "0 30px 60px -25px rgba(11,15,20,0.55), 0 12px 24px -16px rgba(11,15,20,0.30)",
      border: "1px solid var(--border-dark)",
      color: "var(--fg-cream)",
      display: "flex",
      flexDirection: "column"
    }}>
      <div className="glow-blue-dark" />

      {/* header: LIVE + timer */}
      <div style={{
        position: "relative", zIndex: 2,
        display: "flex", alignItems: "center", justifyContent: "space-between",
        padding: "16px 18px",
        borderBottom: "1px solid var(--border-dark)"
      }}>
        <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
          <span className="pulse-dot" />
          <span className="mono" style={{ fontSize: 11, letterSpacing: "0.18em", color: "var(--fg-cream)" }}>
            {p.live}
          </span>
        </div>
        <span className="mono" style={{ fontSize: 12, color: "var(--muted-cream)", fontVariantNumeric: "tabular-nums" }}>
          {mm}:{ss}
        </span>
      </div>

      {/* caller card */}
      <div style={{
        position: "relative", zIndex: 2,
        padding: "16px 18px 8px",
        display: "flex", alignItems: "center", gap: 12
      }}>
        <div style={{
          width: 38, height: 38, borderRadius: 999,
          background: "rgba(96,165,250,0.12)",
          border: "1px solid rgba(96,165,250,0.32)",
          display: "grid", placeItems: "center",
          color: "var(--cat-blue)"
        }}>
          <Icon.Phone size={16} />
        </div>
        <div>
          <div className="display" style={{ fontSize: 17, fontWeight: 600 }}>{p.caller}</div>
          <div style={{ fontSize: 11.5, color: "var(--muted-cream)", marginTop: 2 }}>{p.callerNote}</div>
        </div>
      </div>

      {/* waveform */}
      <div style={{ position: "relative", zIndex: 2, padding: "8px 18px 14px" }}>
        <div style={{
          background: "rgba(155,201,210,0.06)",
          border: "1px solid rgba(155,201,210,0.14)",
          borderRadius: 14,
          padding: "14px 14px",
          display: "flex",
          alignItems: "center",
          justifyContent: "center"
        }}>
          <Waveform bars={36} height={40} color="var(--accent-teal)" />
        </div>
      </div>

      {/* transcript */}
      <div style={{ position: "relative", zIndex: 2, padding: "0 18px", flex: 1, overflow: "hidden" }}>
        <div className="mono" style={{ fontSize: 10, letterSpacing: "0.18em", color: "var(--muted-cream)" }}>
          TRANSCRIPT · LIVE
        </div>
        <div style={{ marginTop: 10, display: "flex", flexDirection: "column", gap: 8 }}>
          {p.transcript.map((line, i) => (
            <div key={i} style={{ display: "flex", gap: 8, fontSize: 12.5, lineHeight: 1.4 }}>
              <span className="mono" style={{ width: 16, flex: "none", color: line.who === "sonaro" ? "var(--accent-teal)" : "var(--muted-cream)" }}>
                {line.who === "sonaro" ? "S" : "P"}
              </span>
              <span style={{ color: line.who === "sonaro" ? "var(--fg-cream)" : "var(--muted-cream)" }}>{line.text}</span>
            </div>
          ))}
          <div style={{ display: "flex", gap: 8, fontSize: 12.5 }}>
            <span className="mono" style={{ width: 16, color: "var(--accent-teal)" }}>S</span>
            <span><span className="typing-dot" style={{ background: "var(--accent-teal)" }} /><span className="typing-dot" style={{ background: "var(--accent-teal)" }} /><span className="typing-dot" style={{ background: "var(--accent-teal)" }} /></span>
          </div>
        </div>
      </div>

      {/* footer: intent / status */}
      <div style={{
        position: "relative", zIndex: 2,
        padding: "14px 18px 16px",
        borderTop: "1px solid var(--border-dark)",
        display: "flex", gap: 16, marginTop: 12
      }}>
        <div style={{ flex: 1 }}>
          <div className="mono" style={{ fontSize: 9.5, letterSpacing: "0.18em", color: "var(--muted-cream)" }}>{p.intentLabel}</div>
          <div style={{ fontSize: 13.5, fontWeight: 500, marginTop: 4 }}>{p.intent}</div>
        </div>
        <div style={{ flex: 1 }}>
          <div className="mono" style={{ fontSize: 9.5, letterSpacing: "0.18em", color: "var(--muted-cream)" }}>{p.statusLabel}</div>
          <div style={{ fontSize: 13.5, fontWeight: 500, marginTop: 4, color: "var(--cat-green)", display: "inline-flex", alignItems: "center", gap: 6 }}>
            <span style={{ width: 6, height: 6, borderRadius: 999, background: "var(--cat-green)" }} />
            {p.status}
          </div>
        </div>
      </div>
    </div>
  );
}

window.SESPreview = SESPreview;
window.VoicePreview = VoicePreview;
window.Waveform = Waveform;
