// Hand-drawn SVG elements — wobbly, marker-like overlays.
// Use sparingly to juxtapose the clean editorial layout.

// All elements use slightly irregular bezier paths to feel hand-made.
// Color defaults to currentColor; pass a color prop to override.

const sketchStroke = {
  fill: "none",
  strokeLinecap: "round",
  strokeLinejoin: "round",
};

// Scribbled oval circle — drawn around full viewBox so it wraps text edges
const SketchCircle = ({ width = 240, height = 90, color = "#1AA5F5", strokeWidth = 3, animate = false, duration = 1100, delay = 200, style }) => {
  const pathRef = React.useRef(null);
  const [ref, inView] = (animate && typeof useInView === "function") ? useInView({ threshold: 0.4 }) : [null, true];
  React.useEffect(() => {
    if (!animate) return;
    const p = pathRef.current;
    if (!p) return;
    const len = p.getTotalLength();
    p.style.strokeDasharray = `${len}`;
    p.style.strokeDashoffset = `${len}`;
    if (inView) {
      // force reflow
      p.getBoundingClientRect();
      p.style.transition = `stroke-dashoffset ${duration}ms cubic-bezier(.45,.05,.55,.95) ${delay}ms`;
      p.style.strokeDashoffset = "0";
    }
  }, [inView, animate, duration, delay]);

  return (
    <svg ref={ref} width={width} height={height} viewBox="0 0 240 90" preserveAspectRatio="none" style={{ overflow: "visible", ...style }}>
      <path
        ref={pathRef}
        d="M 6 46
           C 6 22, 60 6, 120 6
           C 184 6, 234 22, 234 45
           C 234 70, 178 84, 120 84
           C 58 84, 4 72, 4 46
           C 4 24, 56 10, 122 10
           C 188 10, 236 26, 234 48"
        stroke={color}
        strokeWidth={strokeWidth}
        fill="none"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  );
};

// Underline — wobbly stroke, stretches to full container width.
// Pass `double` to render an extra ghost stroke for emphasis.
// `animate` reveals the stroke left-to-right via clip-path (works under preserveAspectRatio="none")
const SketchUnderline = ({ width = 240, height = 22, color = "#1AA5F5", strokeWidth = 3, double = false, animate = false, duration = 800, delay = 250, style }) => {
  const [ref, inView] = (animate && typeof useInView === "function") ? useInView({ threshold: 0.4 }) : [null, true];
  const wrapStyle = animate ? {
    display: "block",
    width: "100%",
    height: "100%",
    overflow: "hidden",
    clipPath: inView ? "inset(0 0 0 0)" : "inset(0 100% 0 0)",
    transition: `clip-path ${duration}ms cubic-bezier(.4,0,.2,1) ${delay}ms`,
  } : { display: "block", width: "100%", height: "100%" };

  return (
    <span ref={ref} style={{ display: "block", width: "100%", height: "100%", ...style }}>
      <span style={wrapStyle}>
        <svg width={width} height={height} viewBox="0 0 240 22" preserveAspectRatio="none" style={{ overflow: "visible", display: "block", width: "100%", height: "100%" }}>
          <path
            d="M 4 12 C 40 6, 80 16, 120 10 C 160 4, 200 14, 236 8"
            stroke={color}
            strokeWidth={strokeWidth}
            vectorEffect="non-scaling-stroke"
            {...sketchStroke}
          />
          {double && (
            <path
              d="M 8 18 C 50 14, 90 20, 132 16 C 174 12, 210 18, 232 16"
              stroke={color}
              strokeWidth={strokeWidth - 1}
              vectorEffect="non-scaling-stroke"
              opacity="0.7"
              {...sketchStroke}
            />
          )}
        </svg>
      </span>
    </span>
  );
};

// Curved arrow pointing right-down (for margin notes)
const SketchArrow = ({ width = 80, height = 80, color = "#0A0A0F", strokeWidth = 2.5, direction = "se", style }) => {
  const paths = {
    se: { d: "M 8 12 C 30 14, 52 28, 60 56", arrow: "M 52 50 L 60 58 L 68 50" },
    sw: { d: "M 72 12 C 50 14, 28 28, 20 56", arrow: "M 12 50 L 20 58 L 28 50" },
    ne: { d: "M 8 68 C 30 66, 52 52, 60 24", arrow: "M 52 30 L 60 22 L 68 30" },
    e:  { d: "M 6 40 C 30 30, 50 50, 70 38", arrow: "M 64 30 L 72 38 L 64 46" },
    w:  { d: "M 74 40 C 50 30, 30 50, 10 38", arrow: "M 16 30 L 8 38 L 16 46" },
  };
  const p = paths[direction] || paths.se;
  return (
    <svg width={width} height={height} viewBox="0 0 80 80" style={{ overflow: "visible", ...style }}>
      <path d={p.d} stroke={color} strokeWidth={strokeWidth} {...sketchStroke} />
      <path d={p.arrow} stroke={color} strokeWidth={strokeWidth} {...sketchStroke} />
    </svg>
  );
};

// Star / sparkle — drawn as overlapping strokes
const SketchStar = ({ size = 36, color = "#1AA5F5", strokeWidth = 2.5, style }) => (
  <svg width={size} height={size} viewBox="0 0 36 36" style={{ overflow: "visible", ...style }}>
    <path d="M 18 4 L 18 32" stroke={color} strokeWidth={strokeWidth} {...sketchStroke} />
    <path d="M 4 18 L 32 18" stroke={color} strokeWidth={strokeWidth} {...sketchStroke} />
    <path d="M 8 8 L 28 28" stroke={color} strokeWidth={strokeWidth - 0.5} {...sketchStroke} />
    <path d="M 28 8 L 8 28" stroke={color} strokeWidth={strokeWidth - 0.5} {...sketchStroke} />
  </svg>
);

// Highlighter swipe — wide chunky behind text
const SketchHighlight = ({ width = 240, height = 36, color = "#1AA5F5", style }) => (
  <svg width={width} height={height} viewBox="0 0 240 36" preserveAspectRatio="none" style={{ overflow: "visible", ...style }}>
    <path
      d="M 4 12 C 60 8, 120 14, 232 10 L 234 28 C 180 32, 100 26, 6 30 Z"
      fill={color}
      opacity="0.35"
    />
  </svg>
);

// Squiggle divider
const SketchSquiggle = ({ width = 320, height = 22, color = "#0A0A0F", strokeWidth = 2.5, style }) => (
  <svg width={width} height={height} viewBox="0 0 320 22" style={{ overflow: "visible", ...style }}>
    <path
      d="M 4 11 Q 24 -2 44 11 T 84 11 T 124 11 T 164 11 T 204 11 T 244 11 T 284 11 T 316 11"
      stroke={color}
      strokeWidth={strokeWidth}
      {...sketchStroke}
    />
  </svg>
);

// Scratch checkmark
const SketchCheck = ({ size = 32, color = "#1AA5F5", strokeWidth = 3, style }) => (
  <svg width={size} height={size} viewBox="0 0 32 32" style={{ overflow: "visible", ...style }}>
    <path d="M 4 18 C 8 20, 12 24, 14 26 C 18 18, 22 10, 30 4" stroke={color} strokeWidth={strokeWidth} {...sketchStroke} />
  </svg>
);

// Asterisk — six radiating wobbly strokes, drawn with multiple imperfect passes
const SketchAsterisk = ({ size = 32, color = "#0A0A0F", strokeWidth = 2.5, animate = false, duration = 700, delay = 0, style }) => {
  const svgRef = React.useRef(null);
  const [ref, inView] = (animate && typeof useInView === "function") ? useInView({ threshold: 0.4 }) : [null, true];

  React.useEffect(() => {
    if (!animate || !svgRef.current) return;
    const paths = svgRef.current.querySelectorAll("path");
    paths.forEach((p, i) => {
      const len = p.getTotalLength();
      p.style.strokeDasharray = `${len}`;
      p.style.strokeDashoffset = `${len}`;
      if (inView) {
        p.getBoundingClientRect();
        p.style.transition = `stroke-dashoffset ${duration}ms cubic-bezier(.4,0,.2,1) ${delay + i * 90}ms`;
        p.style.strokeDashoffset = "0";
      }
    });
  }, [inView, animate, duration, delay]);

  return (
    <svg ref={(node) => { svgRef.current = node; if (ref) ref.current = node; }} width={size} height={size} viewBox="0 0 32 32" style={{ overflow: "visible", ...style }}>
      <g stroke={color} fill="none" strokeLinecap="round" strokeLinejoin="round">
        {/* Vertical — main pass with kinks, plus a faint scratchy second pass */}
        <path d="M 17.0 4.4 C 14.8 7.6, 17.4 10.8, 15.6 14.0 C 14.4 16.4, 17.6 18.8, 16.0 21.4 C 14.8 23.6, 17.0 25.6, 16.0 28.0"
          strokeWidth={strokeWidth} />
        <path d="M 16.4 4.8 C 17.4 7.0, 16.0 10.6, 17.0 13.6 C 17.8 15.8, 15.6 19.0, 16.6 22.0 C 17.0 24.2, 16.0 26.6, 16.4 28.4"
          strokeWidth={strokeWidth * 0.55} opacity="0.5" />
        <path d="M 16.6 5.6 C 16.0 8.6, 16.4 12.4, 16.2 16.0" strokeWidth={strokeWidth * 0.4} opacity="0.35" />

        {/* Down-right diagonal — bowed, broken into two segments */}
        <path d="M 4.6 9.0 C 8.0 10.4, 11.4 12.6, 14.4 14.6 C 15.4 15.4, 16.0 15.8, 16.6 16.2"
          strokeWidth={strokeWidth} />
        <path d="M 16.0 16.0 C 18.6 17.4, 21.4 19.4, 24.0 21.0 C 25.4 21.8, 26.4 22.4, 27.4 23.2"
          strokeWidth={strokeWidth} />
        <path d="M 5.4 10.0 C 11.0 12.4, 16.0 15.0, 21.0 17.8 C 23.6 19.2, 25.0 20.6, 26.4 22.0"
          strokeWidth={strokeWidth * 0.55} opacity="0.5" />

        {/* Down-left diagonal — bowed the other way, also broken */}
        <path d="M 27.4 9.0 C 24.4 10.4, 21.4 12.4, 18.6 14.4 C 17.4 15.2, 16.6 15.8, 16.0 16.2"
          strokeWidth={strokeWidth} />
        <path d="M 16.0 16.0 C 13.0 17.6, 9.8 19.6, 7.0 21.4 C 5.6 22.2, 4.6 22.8, 4.4 23.2"
          strokeWidth={strokeWidth} />
        <path d="M 26.6 10.0 C 21.0 12.6, 16.0 15.0, 11.0 17.6 C 8.4 19.0, 6.8 20.4, 5.4 21.8"
          strokeWidth={strokeWidth * 0.55} opacity="0.5" />
      </g>
    </svg>
  );
};

// Hand-written margin note in a casual handwriting font
const SketchNote = ({ children, rotate = -4, color = "#0A0A0F", style }) => (
  <span style={{
    fontFamily: '"Caveat", "Bradley Hand", "Comic Sans MS", cursive',
    fontSize: 22,
    lineHeight: 1.1,
    color,
    transform: `rotate(${rotate}deg)`,
    display: "inline-block",
    ...style,
  }}>
    {children}
  </span>
);

Object.assign(window, {
  SketchCircle, SketchUnderline, SketchArrow, SketchStar,
  SketchHighlight, SketchSquiggle, SketchCheck, SketchAsterisk, SketchNote,
});
