import type { ReactNode } from "react";

import type { Illust, Tint } from "@/lib/blog/types";
import styles from "./blog.module.css";

// Translucent white strokes/fills from the design's illustration library.
const W1 = "rgba(255,255,255,0.22)";
const W2 = "rgba(255,255,255,0.5)";
const W3 = "rgba(255,255,255,0.85)";

/**
 * Shared gradient defs for the illustrations (design's `#ibrand`). No
 * gradientUnits → objectBoundingBox, so it scales to each shape regardless of
 * viewBox. Render once per page.
 */
export function BlogGradientDefs() {
  return (
    <svg
      width="0"
      height="0"
      aria-hidden="true"
      style={{ position: "absolute" }}
    >
      <defs>
        <linearGradient id="ibrand" x1="0" y1="1" x2="1" y2="0">
          <stop stopColor="#007BFF" />
          <stop offset="0.55" stopColor="#FF5722" />
          <stop offset="1" stopColor="#FBC02D" />
        </linearGradient>
      </defs>
    </svg>
  );
}

// viewBox 0 0 120 90 — ported verbatim from the design's ILL library.
const ILL: Record<Illust, ReactNode> = {
  chevron: (
    <g
      fill="none"
      strokeWidth="8.5"
      strokeLinecap="round"
      strokeLinejoin="round"
    >
      <path d="M30 22 L52 45 L30 68" stroke={W1} />
      <path d="M52 22 L74 45 L52 68" stroke={W2} />
      <path d="M74 22 L96 45 L74 68" stroke="url(#ibrand)" />
    </g>
  ),
  layers: (
    <g strokeLinejoin="round">
      <rect x="34" y="44" width="52" height="20" rx="5" fill={W1} />
      <rect x="34" y="32" width="52" height="20" rx="5" fill={W2} />
      <rect x="34" y="20" width="52" height="20" rx="5" fill="url(#ibrand)" />
    </g>
  ),
  bars: (
    <g>
      <rect x="26" y="54" width="13" height="16" rx="3" fill={W1} />
      <rect x="44" y="42" width="13" height="28" rx="3" fill={W2} />
      <rect x="62" y="30" width="13" height="40" rx="3" fill={W2} />
      <rect x="80" y="18" width="13" height="52" rx="3" fill="url(#ibrand)" />
      <path
        d="M26 40 L48 30 L66 34 L92 14"
        fill="none"
        stroke={W3}
        strokeWidth="3"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
      <circle cx="92" cy="14" r="4.5" fill="#fff" />
    </g>
  ),
  orbit: (
    <g fill="none">
      <ellipse cx="60" cy="45" rx="38" ry="16" stroke={W1} strokeWidth="3" />
      <ellipse cx="60" cy="45" rx="22" ry="34" stroke={W2} strokeWidth="3" />
      <circle cx="60" cy="45" r="10" fill="url(#ibrand)" />
      <circle cx="98" cy="45" r="5" fill="#fff" />
      <circle cx="60" cy="11" r="4" fill={W3} />
    </g>
  ),
  search: (
    <g fill="none" strokeLinecap="round">
      <circle cx="52" cy="40" r="22" stroke="url(#ibrand)" strokeWidth="8" />
      <path d="M70 58 L92 78" stroke={W3} strokeWidth="8" />
      <path
        d="M86 20 l2.5 6 6 2.5 -6 2.5 -2.5 6 -2.5 -6 -6 -2.5 6 -2.5 z"
        fill={W2}
        stroke="none"
      />
    </g>
  ),
  network: (
    <g>
      <line x1="40" y1="26" x2="80" y2="26" stroke={W1} strokeWidth="3" />
      <line x1="40" y1="26" x2="34" y2="62" stroke={W1} strokeWidth="3" />
      <line x1="80" y1="26" x2="86" y2="62" stroke={W1} strokeWidth="3" />
      <line x1="34" y1="62" x2="86" y2="62" stroke={W1} strokeWidth="3" />
      <line x1="40" y1="26" x2="86" y2="62" stroke={W1} strokeWidth="3" />
      <circle cx="40" cy="26" r="9" fill={W2} />
      <circle cx="80" cy="26" r="9" fill="url(#ibrand)" />
      <circle cx="34" cy="62" r="9" fill={W2} />
      <circle cx="86" cy="62" r="9" fill={W3} />
    </g>
  ),
  grid: (
    <g fill="none" strokeWidth="3" strokeLinejoin="round">
      <path
        d="M60 16 L96 34 L60 52 L24 34 Z"
        stroke="url(#ibrand)"
        fill="rgba(255,255,255,0.06)"
      />
      <path d="M24 34 L24 50 L60 68 L60 52" stroke={W2} />
      <path d="M96 34 L96 50 L60 68" stroke={W1} />
      <path d="M42 25 L78 43 M60 16 L60 52" stroke={W1} />
    </g>
  ),
  spark: (
    <g>
      <path
        d="M58 14 c3 18 6 21 24 24 c-18 3 -21 6 -24 24 c-3 -18 -6 -21 -24 -24 c18 -3 21 -6 24 -24 z"
        fill="url(#ibrand)"
      />
      <path
        d="M90 50 c1.6 8 3 9.4 11 11 c-8 1.6 -9.4 3 -11 11 c-1.6 -8 -3 -9.4 -11 -11 c8 -1.6 9.4 -3 11 -11 z"
        fill={W2}
      />
      <path
        d="M30 52 c1.2 6 2.2 7 8 8 c-5.8 1.2 -6.8 2 -8 8 c-1.2 -6 -2.2 -7 -8 -8 c5.8 -1.2 6.8 -2 8 -8 z"
        fill={W1}
      />
    </g>
  ),
  terminal: (
    <g>
      <rect
        x="22"
        y="20"
        width="76"
        height="50"
        rx="7"
        fill="rgba(255,255,255,0.06)"
        stroke={W2}
        strokeWidth="2.5"
      />
      <line x1="22" y1="33" x2="98" y2="33" stroke={W1} strokeWidth="2.5" />
      <circle cx="29" cy="26.5" r="2.4" fill="url(#ibrand)" />
      <circle cx="37" cy="26.5" r="2.4" fill={W1} />
      <path
        d="M32 44 L40 51 L32 58"
        fill="none"
        stroke="url(#ibrand)"
        strokeWidth="3.4"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
      <line
        x1="48"
        y1="58"
        x2="72"
        y2="58"
        stroke={W2}
        strokeWidth="3.4"
        strokeLinecap="round"
      />
    </g>
  ),
};

/**
 * Decorative article thumbnail — tinted gradient panel + brand illustration.
 * `variant` swaps between the standard card thumb and the dark video thumb.
 */
export function BlogThumb({
  illust,
  tint,
  badge,
  className,
  children,
}: {
  illust: Illust;
  tint: Tint;
  badge?: string;
  className?: string;
  children?: ReactNode;
}) {
  return (
    <div
      className={`${styles.thumb} ${className ?? ""}`}
      data-tint={tint}
      aria-hidden={!badge}
    >
      <svg viewBox="0 0 120 90" fill="none" aria-hidden="true">
        <g className={styles.illG}>{ILL[illust]}</g>
      </svg>
      {badge ? <span className={styles.badge}>{badge}</span> : null}
      {children}
    </div>
  );
}
