// Animated drafting mockup, narrates the value of Drafting:
// 1) start from approved template  →  2) guardrails enforce structure
// 3) AI fills a parameter (within playbook limits)  →  4) clause-aware check fires
// 5) co-author joins live  →  6) "Matches playbook · ready to send" + saved-time chip

function DraftingAnimation() {
  const [tick, setTick] = React.useState(0);
  React.useEffect(() => {
    const id = setInterval(() => setTick((t) => (t + 1) % 8), 1100);
    return () => clearInterval(id);
  }, []);
  const show = (n) => tick >= n;

  // Caret blink for the "typing" line
  const [caret, setCaret] = React.useState(true);
  React.useEffect(() => {
    const id = setInterval(() => setCaret((c) => !c), 480);
    return () => clearInterval(id);
  }, []);

  // Phase legend:
  // 0  empty editor with template skeleton
  // 1  clause 3.1 fills in (template-locked)
  // 2  clause 3.2 starts to draft, parameter highlighted
  // 3  AI inserts the playbook value chip "EUR 5,000,000"
  // 4  risk flag slides in: "DPA missing"
  // 5  co-author cursor + comment slide in
  // 6  flag resolves to ✓ matches playbook
  // 7  hold

  const statusDot = show(6) ? "#1F8A4C" : show(4) ? "#D97706" : show(1) ? "#7B61FF" : "var(--ink-5)";
  const statusLabel = show(6) ? "ready to send" : show(4) ? "review pending" : show(1) ? "drafting…" : "template loaded";

  return (
    <div className="hero-mockup-container">
      {/* Background card container with photo of human in office */}
      <div
        style={{
          width: "100%",
          height: "540px",
          border: "1px solid var(--line-strong)",
          borderRadius: "28px",
          boxShadow: "0 20px 40px rgba(14,14,16,0.03), 0 1px 3px rgba(14,14,16,0.01)",
          position: "relative",
          overflow: "hidden",
          display: "flex",
          alignItems: "center",
          justifyContent: "center",
          padding: "24px",
        }}
      >
        {/* Background Image Layer (dedicated photo) */}
        <div
          style={{
            position: "absolute",
            inset: 0,
            backgroundImage: "url('/images/drafting_bg.png')",
            backgroundSize: "cover",
            backgroundPosition: "center",
            zIndex: 0,
            pointerEvents: "none",
          }}
        />
        {/* Subtle white wash overlay to ensure readability */}
        <div
          style={{
            position: "absolute",
            inset: 0,
            background: "rgba(255, 255, 255, 0.12)",
            zIndex: 1,
            pointerEvents: "none",
          }}
        />

        {/* Card wrapper */}
        <div style={{ position: "relative", zIndex: 2, width: "100%", maxWidth: "340px" }}>
          {/* Co-author cursor (slides in over the doc) */}
          <div
            style={{
              position: "absolute",
              top: 132,
              right: 28,
              opacity: show(5) && !show(7) ? 1 : 0,
              transform: show(5) ? "translate(0,0)" : "translate(8px,-6px)",
              transition: "all 360ms cubic-bezier(.2,.8,.2,1)",
              zIndex: 5,
              pointerEvents: "none"
            }}>
            <svg width="14" height="18" viewBox="0 0 14 18" style={{ display: "block", filter: "drop-shadow(0 2px 4px rgba(14,14,16,0.20))" }}>
              <path d="M1 1 L1 14 L4.5 11 L7 16.5 L9 15.5 L6.5 10 L11 10 Z" fill="#E89B3C" stroke="#fff" strokeWidth="1" />
            </svg>
            <div
              style={{
                marginTop: 4,
                background: "#E89B3C",
                color: "#fff",
                fontSize: 10.5,
                fontWeight: 600,
                padding: "3px 7px",
                borderRadius: 4,
                whiteSpace: "nowrap"
              }}>
              Maya · counsel
            </div>
          </div>

      <MockPanel
        title="Draft · MSA v3"
        sub={
        <span style={{ display: "inline-flex", alignItems: "center", gap: 6 }}>
            <span
            style={{
              width: 6, height: 6, borderRadius: "50%",
              background: statusDot,
              boxShadow: show(6) ? "0 0 0 4px rgba(31,138,76,0.18)" : show(4) ? "0 0 0 4px rgba(217,119,6,0.18)" : "none",
              transition: "background 300ms, box-shadow 300ms"
            }} />
          
            {statusLabel}
          </span>
        }>
        
        {/* Template badge */}
        <div
          style={{
            display: "inline-flex",
            alignItems: "center",
            gap: 6,
            fontSize: 10.5,
            fontWeight: 600,
            letterSpacing: "0.06em",
            textTransform: "uppercase",
            color: "var(--accent-deep)",
            background: "rgba(110,86,207,0.10)",
            border: "1px solid rgba(110,86,207,0.25)",
            padding: "4px 8px",
            borderRadius: 999,
            marginBottom: 12
          }}>
          
          <span style={{ width: 6, height: 6, borderRadius: "50%", background: "#7B61FF" }} />
          Template · MSA EU v4 · locked
        </div>

        {/* Doc body */}
        <div
          style={{
            position: "relative",
            padding: 14,
            background: "var(--bg-soft)",
            borderRadius: 8,
            fontFamily: "var(--font-serif)",
            fontSize: 12,
            lineHeight: 1.7,
            color: "var(--ink-2)",
            minHeight: 132
          }}>
          
          {/* Clause 3.1, fills in at tick 1 */}
          <div
            style={{
              opacity: show(1) ? 1 : 0.25,
              transition: "opacity 360ms",
              marginBottom: 10, fontFamily: "Poppins"
            }}>
            
            <span style={{ color: "var(--ink-5)", fontFamily: "var(--font-mono)", fontSize: 10 }}>3.1&nbsp;</span>
            {show(1) ?
            "Each party shall keep Confidential Information confidential for five (5) years." :

            <Skeleton w="92%" />
            }
          </div>

          {/* Clause 3.2, typing animation, then AI parameter chip at tick 3 */}
          <div style={{ position: "relative", fontFamily: "Poppins" }}>
            <span style={{ color: "var(--ink-5)", fontFamily: "var(--font-mono)", fontSize: 10 }}>3.2&nbsp;</span>
            {show(2) ?
            <>
                The Provider shall maintain liability insurance with limits no less than{" "}
                <span
                style={{
                  background: show(3) ? "rgba(124,92,211,0.16)" : "rgba(124,92,211,0.06)",
                  border: show(3) ? "1px solid rgba(124,92,211,0.35)" : "1px dashed rgba(124,92,211,0.30)",
                  padding: "1px 5px",
                  borderRadius: 4,
                  fontFamily: "var(--font-mono)",
                  fontSize: 11,
                  color: show(3) ? "var(--accent-deep)" : "var(--ink-4)",
                  transition: "all 320ms",
                  display: "inline-flex",
                  alignItems: "center",
                  gap: 4
                }}>
                
                  {show(3) &&
                <span
                  style={{
                    fontSize: 8,
                    fontWeight: 700,
                    letterSpacing: "0.06em",
                    color: "#fff",
                    background: "#7B61FF",
                    padding: "1px 4px",
                    borderRadius: 3
                  }}>
                  
                      AI
                    </span>
                }
                  {show(3) ? "EUR 5,000,000" : "amount"}
                </span>{" "}
                per claim
                {show(2) && !show(3) &&
              <span style={{ opacity: caret ? 1 : 0, color: "#7B61FF", marginLeft: 1 }}>▍</span>
              }
                .
              </> :

            <Skeleton w="78%" />
            }
          </div>
        </div>

        {/* Issue / status bar, morphs between states */}
        <div
          style={{
            marginTop: 12,
            padding: "10px 12px",
            borderRadius: 8,
            display: "flex",
            alignItems: "center",
            gap: 10,
            fontSize: 11.5,
            transition: "all 360ms",
            background: show(6) ?
            "rgba(31,138,76,0.10)" :
            show(4) ?
            "rgba(217,119,6,0.10)" :
            "var(--bg-soft)",
            border: show(6) ?
            "1px solid rgba(31,138,76,0.30)" :
            show(4) ?
            "1px solid rgba(217,119,6,0.30)" :
            "1px solid var(--line)",
            color: show(6) ? "var(--success)" : show(4) ? "#B45309" : "var(--ink-4)"
          }}>
          
          {show(6) ?
          <>
              <Check />
              <span>Matches standard terms · ready to send</span>
              <span
              style={{
                marginLeft: "auto",
                fontSize: 10,
                fontWeight: 600,
                letterSpacing: "0.06em",
                textTransform: "uppercase",
                color: "#0F7B3F",
                background: "rgba(15,123,63,0.12)",
                padding: "3px 6px",
                borderRadius: 4
              }}>
              
                0 issues
              </span>
            </> :
          show(4) ?
          <>
              <Warn />
              <span>
                <strong style={{ fontWeight: 600 }}>DPA reference missing</strong> · required for EU vendors
              </span>
              <span
              style={{
                marginLeft: "auto",
                fontSize: 10,
                fontWeight: 600,
                letterSpacing: "0.06em",
                textTransform: "uppercase",
                color: "#B45309",
                background: "rgba(217,119,6,0.12)",
                padding: "3px 6px",
                borderRadius: 4
              }}>
              
                fix
              </span>
            </> :
          show(1) ?
          <>
              <Spinner />
              <span>Checking clause terms…</span>
            </> :

          <>
              <span style={{ width: 14, height: 14, borderRadius: "50%", border: "1px dashed var(--ink-5)" }} />
              <span>Idle · waiting for first clause</span>
            </>
          }
        </div>
      </MockPanel>

      {/* Floating "Compliant by default" chip, appears on success */}
      <div
        style={{
          position: "absolute",
          bottom: -18,
          left: "50%",
          transform: show(6) ? "translate(-50%, 0)" : "translate(-50%, 12px)",
          opacity: show(6) ? 1 : 0,
          transition: "all 480ms cubic-bezier(.2,.8,.2,1)",
          background: "#0E0E10",
          color: "#fff",
          borderRadius: 999,
          padding: "10px 16px",
          fontSize: 12.5,
          fontWeight: 500,
          display: "flex",
          alignItems: "center",
          gap: 10,
          boxShadow: "0 18px 40px -16px rgba(14,14,16,0.45)",
          zIndex: 4,
          whiteSpace: "nowrap"
        }}>
        
        <span style={{ fontSize: 14 }}>✓</span>
        <strong style={{ fontWeight: 600, color: "#C7BFFF" }}>Compliant</strong>
        <span> by default</span>
        <span style={{ width: 1, height: 14, background: "rgba(255,255,255,0.18)" }} />
        <span style={{ color: "#9D96B5" }}>0 missed clauses</span>
      </div>

        </div>
      </div>

      <style>{`
        @keyframes draftSpin {
          to { transform: rotate(360deg); }
        }
      `}</style>
    </div>);

}

function Skeleton({ w }) {
  return (
    <span
      style={{
        display: "inline-block",
        width: w,
        height: 8,
        borderRadius: 4,
        background: "linear-gradient(90deg, rgba(14,14,16,0.06), rgba(14,14,16,0.10), rgba(14,14,16,0.06))",
        verticalAlign: "middle"
      }}></span>);


}

function Check() {
  return (
    <svg width="14" height="14" viewBox="0 0 14 14" style={{ flexShrink: 0 }}>
      <circle cx="7" cy="7" r="6.5" fill="rgba(31,138,76,0.15)" stroke="rgba(31,138,76,0.45)" />
      <path d="M4 7.2 L6.2 9.2 L10 5.2" fill="none" stroke="#1F8A4C" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" />
    </svg>);

}

function Warn() {
  return (
    <svg width="14" height="14" viewBox="0 0 14 14" style={{ flexShrink: 0 }}>
      <path d="M7 1.5 L13 12 L1 12 Z" fill="rgba(217,119,6,0.18)" stroke="rgba(217,119,6,0.50)" strokeLinejoin="round" />
      <path d="M7 5.5 L7 8.5" stroke="#B45309" strokeWidth="1.4" strokeLinecap="round" />
      <circle cx="7" cy="10.2" r="0.8" fill="#B45309" />
    </svg>);

}

function Spinner() {
  return (
    <span
      style={{
        width: 12,
        height: 12,
        borderRadius: "50%",
        border: "1.6px solid rgba(110,86,207,0.25)",
        borderTopColor: "#7B61FF",
        animation: "draftSpin 0.9s linear infinite",
        display: "inline-block"
      }} />);


}

window.DraftingAnimation = DraftingAnimation;