// src/case-illustration.jsx — SVG illustrations for case cards
// Each illustration is industrial/technical, monochrome graphite + accent.

function CaseIllustration({ kind, tag, stats }) {
  let svg = null;
  if (kind === "equipment") svg = <EquipmentSVG />;
  else if (kind === "supplier") svg = <SupplierSVG />;
  else if (kind === "route") svg = <RouteSVG />;

  return (
    <div style={{
      position: "relative",
      aspectRatio: "16/10",
      background: "var(--bg-2)",
      borderBottom: "1px solid var(--line)",
      overflow: "hidden",
    }}>
      {/* grid background */}
      <svg
        width="100%" height="100%"
        viewBox="0 0 400 250"
        preserveAspectRatio="xMidYMid slice"
        style={{ position: "absolute", inset: 0, display: "block" }}
        aria-hidden="true"
      >
        <defs>
          <pattern id={`grid-${kind}`} width="20" height="20" patternUnits="userSpaceOnUse">
            <path d="M 20 0 L 0 0 0 20" fill="none" stroke="var(--line)" strokeWidth="0.5" opacity="0.6" />
          </pattern>
        </defs>
        <rect width="400" height="250" fill={`url(#grid-${kind})`} />
        {svg}
      </svg>

      {/* tag chip */}
      <div style={{
        position: "absolute", top: 16, left: 16,
        display: "inline-flex", alignItems: "center", gap: 8,
        padding: "5px 10px",
        background: "var(--bg-0)",
        border: "1px solid var(--line)",
        borderRadius: 3,
        fontSize: 10,
        fontFamily: "var(--font-ui), ui-sans-serif, sans-serif",
        letterSpacing: "0.05em",
        textTransform: "uppercase",
        color: "var(--text-mid)",
        whiteSpace: "nowrap",
        maxWidth: "calc(100% - 32px)",
      }}>
        <span style={{ width: 5, height: 5, borderRadius: "50%", background: "var(--accent)", flex: "0 0 5px" }} />
        <span style={{ overflow: "hidden", textOverflow: "ellipsis" }}>CASE / {tag}</span>
      </div>

      {/* stats strip */}
      {stats && (
        <div style={{
          position: "absolute", bottom: 0, left: 0, right: 0,
          display: "grid",
          gridTemplateColumns: `repeat(${stats.length}, 1fr)`,
          background: "color-mix(in oklab, var(--bg-0) 85%, transparent)",
          borderTop: "1px solid var(--line)",
          backdropFilter: "blur(6px)",
        }}>
          {stats.map(([k, v], i) => (
            <div key={i} style={{
              padding: "10px 12px",
              borderRight: i < stats.length - 1 ? "1px solid var(--line)" : "none",
            }}>
              <div style={{
                fontSize: 9,
                fontFamily: "var(--font-ui), ui-sans-serif, sans-serif",
                letterSpacing: "0.06em",
                textTransform: "uppercase",
                color: "var(--text-dim)",
                marginBottom: 2,
              }}>{k}</div>
              <div style={{
                fontSize: 14,
                fontWeight: 600,
                letterSpacing: "-0.01em",
                color: "var(--text)",
              }}>{v}</div>
            </div>
          ))}
        </div>
      )}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// EquipmentSVG — industrial machine schematic (technical drawing)
// ─────────────────────────────────────────────────────────────
function EquipmentSVG() {
  return (
    <g>
      {/* Crosshair marks */}
      <g stroke="var(--text-dim)" strokeWidth="0.5" opacity="0.4">
        <line x1="0" y1="60" x2="400" y2="60" strokeDasharray="2 4" />
        <line x1="0" y1="180" x2="400" y2="180" strokeDasharray="2 4" />
      </g>

      {/* Industrial machine — schematic */}
      <g transform="translate(110, 70)">
        {/* base */}
        <rect x="0" y="80" width="180" height="14" fill="var(--bg-1)" stroke="var(--text-mid)" strokeWidth="1" />
        <rect x="6" y="94" width="14" height="14" fill="var(--bg-1)" stroke="var(--text-mid)" strokeWidth="1" />
        <rect x="160" y="94" width="14" height="14" fill="var(--bg-1)" stroke="var(--text-mid)" strokeWidth="1" />

        {/* body */}
        <rect x="22" y="22" width="136" height="58" fill="var(--bg-0)" stroke="var(--text-mid)" strokeWidth="1.2" />
        <rect x="32" y="32" width="56" height="38" fill="none" stroke="var(--text-dim)" strokeWidth="0.8" />
        <rect x="96" y="32" width="52" height="14" fill="none" stroke="var(--text-dim)" strokeWidth="0.8" />
        <rect x="96" y="50" width="52" height="20" fill="none" stroke="var(--text-dim)" strokeWidth="0.8" />

        {/* dial */}
        <circle cx="60" cy="51" r="14" fill="var(--bg-1)" stroke="var(--text-mid)" strokeWidth="1" />
        <circle cx="60" cy="51" r="9" fill="none" stroke="var(--accent)" strokeWidth="1.5" strokeDasharray="40 30" transform="rotate(-90 60 51)" />
        <circle cx="60" cy="51" r="1.5" fill="var(--accent)" />

        {/* top stack */}
        <rect x="60" y="6" width="60" height="16" fill="var(--bg-0)" stroke="var(--text-mid)" strokeWidth="1" />
        <line x1="68" y1="6" x2="68" y2="0" stroke="var(--text-mid)" strokeWidth="1" />
        <line x1="112" y1="6" x2="112" y2="0" stroke="var(--text-mid)" strokeWidth="1" />
        <circle cx="68" cy="-2" r="3" fill="var(--accent)" />
        <circle cx="112" cy="-2" r="3" fill="var(--bg-1)" stroke="var(--text-mid)" strokeWidth="1" />

        {/* arm */}
        <line x1="22" y1="51" x2="2" y2="51" stroke="var(--text-mid)" strokeWidth="1.2" />
        <circle cx="2" cy="51" r="2.5" fill="var(--bg-1)" stroke="var(--accent)" strokeWidth="1.2" />

        {/* labels */}
        <text x="100" y="40" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="6" fill="var(--text-dim)" letterSpacing="0.5">M-04</text>
        <text x="100" y="60" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="6" fill="var(--text-dim)" letterSpacing="0.5">EU-CE</text>
      </g>

      {/* Dimension lines */}
      <g stroke="var(--accent)" strokeWidth="1" fill="none">
        <line x1="100" y1="62" x2="100" y2="172" />
        <line x1="96" y1="62" x2="104" y2="62" />
        <line x1="96" y1="172" x2="104" y2="172" />
      </g>
      <text x="84" y="120" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="7" fill="var(--accent)" textAnchor="middle" transform="rotate(-90 84 120)">1240 mm</text>

      {/* route indicator EU → CIS */}
      <g transform="translate(310, 20)">
        <rect x="0" y="0" width="74" height="40" fill="var(--bg-0)" stroke="var(--line)" strokeWidth="0.8" />
        <text x="6" y="11" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="6" fill="var(--text-dim)" letterSpacing="0.5">ORIGIN</text>
        <text x="6" y="22" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="8" fill="var(--text)" letterSpacing="0.5">EU</text>
        <line x1="22" y1="20" x2="42" y2="20" stroke="var(--accent)" strokeWidth="1.2" />
        <path d="M 38 17 L 42 20 L 38 23" fill="none" stroke="var(--accent)" strokeWidth="1.2" />
        <text x="48" y="22" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="8" fill="var(--accent)" letterSpacing="0.5">CIS</text>
        <text x="6" y="34" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="6" fill="var(--text-dim)" letterSpacing="0.5">HS 84.79</text>
      </g>
    </g>
  );
}

// ─────────────────────────────────────────────────────────────
// SupplierSVG — supplier network / shortlist matrix
// ─────────────────────────────────────────────────────────────
function SupplierSVG() {
  // 4x3 grid of supplier nodes; 3 are highlighted (shortlist)
  const cells = [];
  const cols = 6, rows = 4;
  // Build dot grid
  for (let r = 0; r < rows; r++) {
    for (let c = 0; c < cols; c++) {
      cells.push({ c, r });
    }
  }
  // shortlist positions (will be in accent + linked)
  const shortlist = [
    { c: 1, r: 1, label: "S-A" },
    { c: 3, r: 0, label: "S-B" },
    { c: 4, r: 2, label: "S-C" },
  ];
  const dropped = { c: 2, r: 3, label: "S-X" };

  const cellW = 50, cellH = 36, ox = 70, oy = 60;
  const px = (c) => ox + c * cellW;
  const py = (r) => oy + r * cellH;

  return (
    <g>
      {/* axes / labels */}
      <text x="20" y="56" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="7" fill="var(--text-dim)" letterSpacing="0.5">МАТРИЦА</text>
      <text x="20" y="68" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="7" fill="var(--text-dim)" letterSpacing="0.5">ПОСТАВЩИКОВ</text>

      <text x="20" y="96" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="6" fill="var(--text-dim)">CRITERIA</text>
      <text x="20" y="130" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="6" fill="var(--text-dim)">PRICE</text>
      <text x="20" y="166" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="6" fill="var(--text-dim)">LEAD-T</text>
      <text x="20" y="202" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="6" fill="var(--text-dim)">DELIV</text>

      {/* dot grid (non-shortlist) */}
      {cells.map(({ c, r }, i) => {
        const isShort = shortlist.some((s) => s.c === c && s.r === r);
        const isDropped = dropped.c === c && dropped.r === r;
        if (isShort || isDropped) return null;
        return (
          <circle key={i} cx={px(c)} cy={py(r)} r="2.5" fill="var(--text-dim)" opacity="0.4" />
        );
      })}

      {/* connection lines between shortlist */}
      <g stroke="var(--accent)" strokeWidth="1" strokeDasharray="2 2" fill="none">
        <line x1={px(shortlist[0].c)} y1={py(shortlist[0].r)} x2={px(shortlist[1].c)} y2={py(shortlist[1].r)} />
        <line x1={px(shortlist[1].c)} y1={py(shortlist[1].r)} x2={px(shortlist[2].c)} y2={py(shortlist[2].r)} />
        <line x1={px(shortlist[0].c)} y1={py(shortlist[0].r)} x2={px(shortlist[2].c)} y2={py(shortlist[2].r)} />
      </g>

      {/* shortlist nodes */}
      {shortlist.map((s, i) => (
        <g key={i}>
          <circle cx={px(s.c)} cy={py(s.r)} r="10" fill="var(--bg-0)" stroke="var(--accent)" strokeWidth="1.5" />
          <circle cx={px(s.c)} cy={py(s.r)} r="4" fill="var(--accent)" />
          <text x={px(s.c)} y={py(s.r) - 14} fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="7" fill="var(--accent)" textAnchor="middle" letterSpacing="0.5">{s.label}</text>
        </g>
      ))}

      {/* dropped node */}
      <g>
        <circle cx={px(dropped.c)} cy={py(dropped.r)} r="9" fill="none" stroke="var(--text-dim)" strokeWidth="1" strokeDasharray="2 2" />
        <line x1={px(dropped.c) - 6} y1={py(dropped.r) - 6} x2={px(dropped.c) + 6} y2={py(dropped.r) + 6} stroke="var(--text-dim)" strokeWidth="1" />
        <line x1={px(dropped.c) - 6} y1={py(dropped.r) + 6} x2={px(dropped.c) + 6} y2={py(dropped.r) - 6} stroke="var(--text-dim)" strokeWidth="1" />
        <text x={px(dropped.c)} y={py(dropped.r) + 18} fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="6" fill="var(--text-dim)" textAnchor="middle">DROPPED</text>
      </g>

      {/* result counter top-right */}
      <g transform="translate(310, 70)">
        <rect x="0" y="0" width="74" height="58" fill="var(--bg-0)" stroke="var(--line)" strokeWidth="0.8" />
        <text x="6" y="11" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="6" fill="var(--text-dim)" letterSpacing="0.5">SHORTLIST</text>
        <text x="6" y="38" fontFamily="var(--font-ui), sans-serif" fontSize="26" fill="var(--accent)" fontWeight="600" letterSpacing="-0.04em">3</text>
        <text x="38" y="38" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="9" fill="var(--text-dim)" letterSpacing="0.5">/ 27</text>
        <line x1="6" y1="44" x2="68" y2="44" stroke="var(--line)" strokeWidth="0.5" />
        <text x="6" y="53" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="6" fill="var(--text-dim)" letterSpacing="0.4">CHECKED</text>
      </g>
    </g>
  );
}

// ─────────────────────────────────────────────────────────────
// RouteSVG — multimodal logistics route (map abstraction)
// ─────────────────────────────────────────────────────────────
function RouteSVG() {
  return (
    <g>
      {/* world abstraction — horizontal latitude lines */}
      <g stroke="var(--text-dim)" strokeWidth="0.4" opacity="0.4">
        <path d="M 0 100 Q 200 80 400 100" fill="none" />
        <path d="M 0 140 Q 200 160 400 140" fill="none" />
      </g>

      {/* route polyline */}
      <g fill="none">
        <path
          d="M 50 150 L 110 110 L 180 130 L 250 90 L 320 130 L 360 100"
          stroke="var(--text-dim)"
          strokeWidth="1"
          strokeDasharray="3 3"
          opacity="0.6"
        />
        <path
          d="M 50 150 L 110 110 L 180 130 L 250 90 L 320 130 L 360 100"
          stroke="var(--accent)"
          strokeWidth="2"
          strokeLinecap="round"
          strokeLinejoin="round"
        />
      </g>

      {/* hub nodes */}
      {[
        { x: 50, y: 150, label: "ORIGIN", mode: "FCT", primary: true },
        { x: 110, y: 110, label: "HUB-1", mode: "TRUCK" },
        { x: 180, y: 130, label: "HUB-2", mode: "RAIL" },
        { x: 250, y: 90, label: "HUB-3", mode: "SEA" },
        { x: 320, y: 130, label: "HUB-4", mode: "RAIL" },
        { x: 360, y: 100, label: "DEST", mode: "WH", primary: true },
      ].map((n, i) => (
        <g key={i}>
          {n.primary ? (
            <>
              <rect x={n.x - 8} y={n.y - 8} width="16" height="16" fill="var(--bg-0)" stroke="var(--accent)" strokeWidth="1.5" />
              <rect x={n.x - 4} y={n.y - 4} width="8" height="8" fill="var(--accent)" />
            </>
          ) : (
            <>
              <circle cx={n.x} cy={n.y} r="5" fill="var(--bg-0)" stroke="var(--accent)" strokeWidth="1.2" />
              <circle cx={n.x} cy={n.y} r="2" fill="var(--accent)" />
            </>
          )}
          <text
            x={n.x}
            y={n.y - 14}
            fontFamily="var(--font-ui), ui-sans-serif, sans-serif"
            fontSize="6"
            fill="var(--text-dim)"
            textAnchor="middle"
            letterSpacing="0.4"
          >
            {n.label}
          </text>
          <text
            x={n.x}
            y={n.y + 18}
            fontFamily="var(--font-ui), ui-sans-serif, sans-serif"
            fontSize="6"
            fill="var(--accent)"
            textAnchor="middle"
            letterSpacing="0.4"
          >
            {n.mode}
          </text>
        </g>
      ))}

      {/* "old route" alternative — crossed-out */}
      <g opacity="0.5">
        <path
          d="M 50 150 Q 200 200 360 100"
          fill="none"
          stroke="var(--text-dim)"
          strokeWidth="1"
          strokeDasharray="4 4"
        />
        <text x="200" y="218" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="7" fill="var(--text-dim)" textAnchor="middle" letterSpacing="0.4">
          OLD ROUTE · CLOSED
        </text>
        <line x1="180" y1="206" x2="220" y2="206" stroke="var(--text-dim)" strokeWidth="0.8" />
      </g>

      {/* delta badge */}
      <g transform="translate(310, 18)">
        <rect x="0" y="0" width="74" height="40" fill="var(--bg-0)" stroke="var(--line)" strokeWidth="0.8" />
        <text x="6" y="11" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="6" fill="var(--text-dim)" letterSpacing="0.5">Δ TIME</text>
        <text x="6" y="30" fontFamily="var(--font-ui), sans-serif" fontSize="16" fill="var(--accent)" fontWeight="600" letterSpacing="-0.02em">−9д</text>
        <text x="38" y="30" fontFamily="var(--font-ui), ui-sans-serif, sans-serif" fontSize="6" fill="var(--text-dim)" letterSpacing="0.4">vs. plan</text>
      </g>
    </g>
  );
}

Object.assign(window, { CaseIllustration });
