/* global React, useLang */
// Visuales de la página Industrias: una "UI ficticia de FORSVAR" por vertical, con la misma
// gramática que los de parts-2.jsx (cabecera, tres tarjetas — escena, señales, decisión — y pie).
// Se cargan sólo en industries.html y se registran en window.FEAT_VISUALS, que FeatRow consulta
// por visualKind. Textos en viz_messages.py (claves viz.ind.* y viz.i<vertical>.*).

// ---------- piezas comunes ----------
function IvShell({ p, grid, children }) {
  const { t } = useLang();
  return (
    <div className="kyc-visual">
      <div className="kyc-head">
        <span className="kyc-tag">{t(p + "tag")}</span>
        <span className="kyc-status"><span className="kyc-dot" /> {t(p + "status")}</span>
      </div>
      <div className={"kyc-grid " + (grid || "iv-grid")}>{children}</div>
      <div className="kyc-footer">
        <span>{t(p + "f1")}</span>
        <span>{t(p + "f2")}</span>
        <span>{t(p + "f3")}</span>
      </div>
    </div>);
}

function IvCard({ label, className, children }) {
  return (
    <div className={"kyc-card " + (className || "")}>
      <div className="kyc-card-label">{label}</div>
      {children}
    </div>);
}

// items: [["flag" | "ok", texto], ...]
function IvSignals({ items }) {
  return (
    <div className="device-signals">
      {items.map(([kind, text], i) =>
        <div key={i} className={"device-signal " + kind}>
          <span className="device-signal-mark">{kind === "ok" ? "✓" : "!"}</span> {text}
        </div>
      )}
    </div>);
}

function IvRules({ items }) {
  return (
    <div className="risk-rules">
      {items.map((r, i) =>
        <div key={i} className={"risk-rule" + (r.hit ? " triggered" : "")}>
          <div className="risk-rule-head">
            <span className={"risk-rule-tag " + r.tagClass}>{r.tag}</span>
            <span className={"risk-rule-status " + (r.hit ? "flag" : "ok")}>{r.hit ? "!" : "✓"}</span>
          </div>
          <div className="risk-rule-name">{r.name}</div>
          <div className="risk-rule-cond">{r.cond}</div>
        </div>
      )}
    </div>);
}

const RULE_VELOCITY = { tag: "Velocity", tagClass: "tag-velocity" };
const RULE_ADVANCED = { tag: "Advanced", tagClass: "tag-advanced" };

function IvDecision({ score, aml, pill, pillKind, rows }) {
  const { t } = useLang();
  const off = (163.4 * (1 - score / 100)).toFixed(1);
  return (
    <IvCard label={t("viz.ind.c3")} className="kyc-decision">
      <div className="kyc-decision-body">
        <div className="kyc-score">
          <div className="kyc-score-ring">
            <svg viewBox="0 0 60 60" width="60" height="60">
              <circle cx="30" cy="30" r="26" fill="none" stroke="var(--line)" strokeWidth="4" />
              <circle cx="30" cy="30" r="26" fill="none" stroke="var(--accent)" strokeWidth="4" strokeDasharray="163.4" strokeDashoffset={off} transform="rotate(-90 30 30)" strokeLinecap="round" />
            </svg>
            <span className="kyc-score-num">{score}</span>
          </div>
          <div>
            <div className="kyc-score-label">{t(aml ? "viz.ind.riskAml" : "viz.ind.risk")}</div>
            <div className={"kyc-pill " + pillKind}>{pill}</div>
          </div>
        </div>
        <div className="kyc-meta-rows">
          {rows.map(([l, v], i) => <div key={i}><span>{l}</span><span>{v}</span></div>)}
        </div>
      </div>
    </IvCard>);
}

// items: [[hora, texto, marcado], ...]
function IvTimeline({ items }) {
  return (
    <div className="iv-timeline">
      {items.map(([time, text, flag], i) =>
        <div key={i} className={"iv-tl-item" + (flag ? " flag" : "")}>
          <span className="iv-tl-time">{time}</span>
          <span className="iv-tl-dot" />
          <span className="iv-tl-text">{text}</span>
        </div>
      )}
    </div>);
}

const MONO = { fontFamily: "JetBrains Mono" };
const RED = "rgba(140,40,50,0.95)";

// ---------- Billeteras digitales — red de cuentas mule ----------
function IndWalletVisual() {
  const { t } = useLang();
  const p = "viz.iwal.";
  const spokes = [
    [80, 34, "$540"], [137, 67, "$520"], [137, 133, "$560"],
    [80, 166, "$530"], [23, 133, "$550"], [23, 67, "$540"]];
  return (
    <IvShell p={p}>
      <IvCard label={t(p + "c1")}>
        <div className="tx-graph">
          <svg viewBox="0 0 160 200" className="tx-svg" aria-hidden="true">
            {spokes.map(([x, y, amt], i) =>
              <g key={i}>
                <line x1="80" y1="100" x2={x} y2={y} stroke="var(--accent)" strokeWidth="1.2" strokeDasharray="3 3" />
                <text x={(80 + x) / 2 + (x === 80 ? 14 : x > 80 ? 7 : -7)} y={(100 + y) / 2 + (x === 80 ? 3 : y < 100 ? -4 : 10)} {...MONO} fontSize="8" textAnchor="middle" fill="var(--neutral-2)">{amt}</text>
                <circle cx={x} cy={y} r="12" fill="#fff" stroke={i < 4 ? "var(--accent)" : "var(--line-strong)"} />
                <text x={x} y={y + 3} {...MONO} fontSize="8" textAnchor="middle" fill="var(--ink)">M{i + 1}</text>
              </g>
            )}
            <circle cx="80" cy="100" r="21" fill="#fff" />
            <circle cx="80" cy="100" r="21" fill="var(--accent-soft)" stroke="var(--accent)" strokeWidth="1.4" />
            <text x="80" y="96" {...MONO} fontSize="7" textAnchor="middle" fill="var(--neutral-2)">{t(p + "origin")}</text>
            <text x="80" y="109" {...MONO} fontSize="8.5" textAnchor="middle" fill="var(--ink)" fontWeight="600">W-4821</text>
          </svg>
          <div className="tx-graph-legend">
            <span><span className="tx-leg-line susp" /> {t(p + "lg1")}</span>
            <span>{t(p + "lg2")}</span>
          </div>
        </div>
      </IvCard>
      <IvCard label={t("viz.ind.signals")}>
        <IvSignals items={[
          ["flag", t(p + "s1")], ["flag", t(p + "s2")], ["flag", t(p + "s3")], ["ok", t(p + "s4")]]} />
      </IvCard>
      <IvDecision score={88} pill={t(p + "pill")} pillKind="reject"
        rows={[[t(p + "m1l"), t(p + "m1v")], [t(p + "m2l"), t(p + "m2v")], [t("viz.ind.case"), "CSE-41K8QD"]]} />
    </IvShell>);
}

// ---------- Procesadores de pago — card testing ----------
function IndProcessorVisual() {
  const { t } = useLang();
  const p = "viz.iprc.";
  const bars = [2, 1, 3, 2, 2, 6, 11, 14, 16, 15, 17, 18];
  return (
    <IvShell p={p}>
      <IvCard label={t(p + "c1")}>
        <div className="tx-graph">
          <svg viewBox="0 0 160 200" className="tx-svg" aria-hidden="true">
            <line x1="8" y1="100" x2="152" y2="100" stroke="var(--accent)" strokeWidth="1" strokeDasharray="3 3" />
            <text x="10" y="93" {...MONO} fontSize="8" fill="var(--accent)">{t(p + "thr")}</text>
            {bars.map((b, i) => {
              const h = b * 7.5;
              return <rect key={i} x={10 + i * 12} y={175 - h} width="8" height={h} rx="1.5"
                fill={b > 10 ? "var(--accent)" : "var(--line-strong)"} />;
            })}
            <line x1="8" y1="175" x2="152" y2="175" stroke="var(--line-strong)" />
            <text x="10" y="188" {...MONO} fontSize="7.5" fill="var(--neutral-3)">-120 s</text>
            <text x="150" y="188" {...MONO} fontSize="7.5" textAnchor="end" fill="var(--neutral-3)">{t(p + "now")}</text>
          </svg>
          <div className="tx-graph-legend">
            <span>{t(p + "lg1")}</span>
            <span>{t(p + "lg2")}</span>
          </div>
        </div>
      </IvCard>
      <IvCard label={t("viz.ind.rules")}>
        <IvRules items={[
          { ...RULE_VELOCITY, hit: true, name: t(p + "rn1"), cond: "count(bin) > 10 · 10 s" },
          { ...RULE_VELOCITY, hit: true, name: t(p + "rn2"), cond: "amount < 2 · ≥ 20 tx" },
          { ...RULE_ADVANCED, hit: false, name: t(p + "rn3"), cond: "merchant_age > 180 d" }]} />
      </IvCard>
      <IvDecision score={93} pill={t(p + "pill")} pillKind="reject"
        rows={[[t(p + "m1l"), "BIN + device"], [t(p + "m2l"), t(p + "m2v")], [t(p + "m3l"), t(p + "m3v")]]} />
    </IvShell>);
}

// ---------- Marketplaces — vendedor vinculado a uno bloqueado ----------
function IndMarketplaceVisual() {
  const { t } = useLang();
  const p = "viz.imkt.";
  return (
    <IvShell p={p}>
      <IvCard label={t(p + "c1")}>
        <div className="tx-graph">
          <svg viewBox="0 0 160 200" className="tx-svg" aria-hidden="true">
            <line x1="42" y1="70" x2="80" y2="146" stroke="var(--accent)" strokeWidth="1.2" strokeDasharray="3 3" />
            <line x1="118" y1="70" x2="80" y2="146" stroke="var(--accent)" strokeWidth="1.2" strokeDasharray="3 3" />
            <rect x="8" y="34" width="68" height="36" rx="5" fill="#fff" stroke="var(--line-strong)" />
            <text x="42" y="50" {...MONO} fontSize="8.5" textAnchor="middle" fill="var(--ink)" fontWeight="600">S-9034</text>
            <text x="42" y="62" {...MONO} fontSize="7" textAnchor="middle" fill="var(--neutral-3)">{t(p + "n1")}</text>
            <rect x="84" y="34" width="68" height="36" rx="5" fill="rgba(180,60,70,0.08)" stroke="rgba(180,60,70,0.45)" />
            <text x="118" y="50" {...MONO} fontSize="8.5" textAnchor="middle" fill={RED} fontWeight="600">S-7710</text>
            <text x="118" y="62" {...MONO} fontSize="7" textAnchor="middle" fill={RED}>{t(p + "n2")}</text>
            <rect x="30" y="134" width="100" height="26" rx="13" fill="#fff" />
            <rect x="30" y="134" width="100" height="26" rx="13" fill="var(--accent-soft)" stroke="var(--accent)" />
            <text x="80" y="150" {...MONO} fontSize="8.5" textAnchor="middle" fill="var(--ink)">{t(p + "acct")}</text>
            <text x="80" y="178" {...MONO} fontSize="7" textAnchor="middle" fill="var(--neutral-3)">{t(p + "acctl")}</text>
          </svg>
          <div className="tx-graph-legend">
            <span><span className="tx-leg-line susp" /> {t(p + "lg1")}</span>
          </div>
        </div>
      </IvCard>
      <IvCard label={t("viz.ind.signals")}>
        <IvSignals items={[
          ["flag", t(p + "s1")], ["flag", t(p + "s2")], ["flag", t(p + "s3")], ["ok", t(p + "s4")]]} />
      </IvCard>
      <IvDecision score={79} pill={t(p + "pill")} pillKind="alert"
        rows={[[t(p + "m1l"), t(p + "m1v")], [t(p + "m2l"), t(p + "m2v")], [t("viz.ind.case"), "CSE-7P2ZRA"]]} />
    </IvShell>);
}

// ---------- E-commerce — checkout card-not-present ----------
function IndEcommerceVisual() {
  const { t } = useLang();
  const p = "viz.ieco.";
  return (
    <IvShell p={p}>
      <IvCard label={t(p + "c1")}>
        <div className="pay-body">
          <div className="pay-amount">$389<em>.90</em><span className="pay-cur">USD</span></div>
          <div className="pay-card">MASTERCARD · ••••9031</div>
          <div className="kyc-meta-rows">
            <div><span>{t(p + "m1l")}</span><span>{t(p + "m1v")}</span></div>
            <div><span>{t(p + "m2l")}</span><span>Montevideo</span></div>
            <div><span>{t(p + "m3l")}</span><span>Asunción</span></div>
          </div>
        </div>
      </IvCard>
      <IvCard label={t(p + "c2")}>
        <IvSignals items={[
          ["flag", t(p + "s1")], ["flag", t(p + "s2")], ["ok", t(p + "s3")], ["ok", t(p + "s4")]]} />
      </IvCard>
      <IvDecision score={58} pill={t(p + "pill")} pillKind="alert"
        rows={[[t(p + "d1l"), t(p + "d1v")], [t(p + "d2l"), t(p + "d2v")], [t(p + "d3l"), t(p + "d3v")]]} />
    </IvShell>);
}

// ---------- Préstamos — identidad sintética ----------
function IndLendingVisual() {
  const { t } = useLang();
  const p = "viz.ilnd.";
  const bars = [
    [t(p + "b1"), 6, "4 d"], [t(p + "b2"), 10, "9 d"],
    [t(p + "b3"), 20, "2 / 10"], [t(p + "b4"), 97, "97%"]];
  return (
    <IvShell p={p}>
      <IvCard label={t(p + "c1")}>
        <div className="pay-body">
          <div className="pay-amount">{t(p + "amt")}<span className="pay-cur">{t(p + "inst")}</span></div>
          <div className="pay-card">{t(p + "id")}</div>
          <div className="kyc-meta-rows">
            <div><span>{t(p + "m1l")}</span><span>{t(p + "m1v")}</span></div>
            <div><span>{t(p + "m2l")}</span><span>App · Android</span></div>
            <div><span>{t(p + "m3l")}</span><span>3 · 24 h</span></div>
          </div>
        </div>
      </IvCard>
      <IvCard label={t(p + "c2")}>
        <div className="iv-bars">
          {bars.map(([l, pct, v], i) =>
            <div key={i} className="iv-bar-row">
              <span className="iv-bar-l">{l}</span>
              <div className="kyc-bar"><div className={"kyc-bar-fill" + (pct < 50 ? " low" : "")} style={{ width: pct + "%" }} /></div>
              <span className="iv-bar-v">{v}</span>
            </div>
          )}
        </div>
        <div className="iv-note">{t(p + "note")}</div>
      </IvCard>
      <IvDecision score={82} pill={t(p + "pill")} pillKind="reject"
        rows={[[t(p + "d1l"), t(p + "d1v")], ["Device", t(p + "d2v")], [t(p + "d3l"), "SOL-55021"]]} />
    </IvShell>);
}

// ---------- Gaming — multicuenta y abuso de promociones ----------
function IndGamingVisual() {
  const { t } = useLang();
  const p = "viz.igam.";
  const accts = [[40, 40, "U-1182"], [120, 40, "U-1190"], [40, 160, "U-1203"], [120, 160, "U-1207"]];
  return (
    <IvShell p={p}>
      <IvCard label={t(p + "c1")}>
        <div className="tx-graph">
          <svg viewBox="0 0 160 200" className="tx-svg" aria-hidden="true">
            {accts.map(([x, y, id], i) =>
              <g key={i}>
                <line x1="80" y1="100" x2={x} y2={y} stroke="var(--accent)" strokeWidth="1.2" strokeDasharray="3 3" />
                <rect x={x - 30} y={y - 13} width="60" height="26" rx="4" fill="#fff" stroke="var(--line-strong)" />
                <text x={x} y={y - 1} {...MONO} fontSize="8.5" textAnchor="middle" fill="var(--ink)">{id}</text>
                <text x={x} y={y + 9} {...MONO} fontSize="6.5" textAnchor="middle" fill="var(--accent)">{t(p + "bonus")}</text>
              </g>
            )}
            <rect x="44" y="84" width="72" height="32" rx="6" fill="#fff" />
            <rect x="44" y="84" width="72" height="32" rx="6" fill="var(--accent-soft)" stroke="var(--accent)" strokeWidth="1.2" />
            <text x="80" y="97" {...MONO} fontSize="7" textAnchor="middle" fill="var(--neutral-2)">DEVICE</text>
            <text x="80" y="109" {...MONO} fontSize="8.5" textAnchor="middle" fill="var(--ink)" fontWeight="600">a91f·77c</text>
          </svg>
          <div className="tx-graph-legend">
            <span>{t(p + "lg1")}</span>
          </div>
        </div>
      </IvCard>
      <IvCard label={t("viz.ind.rules")}>
        <IvRules items={[
          { ...RULE_VELOCITY, hit: true, name: t(p + "rn1"), cond: "count(account) by device > 1" },
          { ...RULE_VELOCITY, hit: true, name: t(p + "rn2"), cond: t(p + "rc2") },
          { ...RULE_ADVANCED, hit: false, name: t(p + "rn3"), cond: t(p + "rc3") }]} />
      </IvCard>
      <IvDecision score={76} pill={t(p + "pill")} pillKind="alert"
        rows={[[t(p + "d1l"), t(p + "d1v")], [t(p + "d2l"), "$200"], [t(p + "d3l"), t(p + "d3v")]]} />
    </IvShell>);
}

// ---------- Gambling — ciclo depósito / apuesta mínima / retiro ----------
function IndGamblingVisual() {
  const { t } = useLang();
  const p = "viz.igbl.";
  return (
    <IvShell p={p}>
      <IvCard label={t(p + "c1")}>
        <IvTimeline items={[
          ["10:02", t(p + "e1"), false],
          ["10:05", t(p + "e2"), true],
          ["10:07", t(p + "e3"), true],
          ["10:09", t(p + "e4"), true]]} />
      </IvCard>
      <IvCard label={t(p + "c2")}>
        <IvSignals items={[
          ["flag", t(p + "s1")], ["flag", t(p + "s2")], ["flag", t(p + "s3")], ["ok", t(p + "s4")]]} />
      </IvCard>
      <IvDecision score={85} aml pill={t(p + "pill")} pillKind="reject"
        rows={[[t(p + "d1l"), t(p + "d1v")], [t(p + "d2l"), t(p + "d2v")], [t("viz.ind.case"), "CSE-3XW9TB"]]} />
    </IvShell>);
}

// ---------- Remesas — fraccionamiento bajo el umbral ----------
function IndRemittanceVisual() {
  const { t } = useLang();
  const p = "viz.irem.";
  const sends = [960, 985, 940, 990, 975, 995];
  // Eje truncado desde 800: lo que importa es lo cerca del umbral que queda cada envío.
  const y = (v) => 175 - (v - 800) * 0.6;
  return (
    <IvShell p={p}>
      <IvCard label={t(p + "c1")}>
        <div className="tx-graph">
          <svg viewBox="0 0 160 200" className="tx-svg" aria-hidden="true">
            <line x1="8" y1={y(1000)} x2="152" y2={y(1000)} stroke="rgba(180,60,70,0.6)" strokeWidth="1" strokeDasharray="3 3" />
            <text x="10" y={y(1000) - 7} {...MONO} fontSize="8" fill={RED}>{t(p + "thr")}</text>
            {sends.map((v, i) =>
              <g key={i}>
                <rect x={12 + i * 23} y={y(v)} width="17" height={175 - y(v)} rx="2" fill="var(--accent)" opacity={0.6 + i * 0.07} />
                <text x={20.5 + i * 23} y={y(v) + 11} {...MONO} fontSize="7" textAnchor="middle" fill="#fff" fontWeight="600">{v}</text>
              </g>
            )}
            <line x1="8" y1="175" x2="152" y2="175" stroke="var(--line-strong)" />
            <text x="10" y="188" {...MONO} fontSize="7.5" fill="var(--neutral-3)">{t(p + "ax1")}</text>
            <text x="150" y="188" {...MONO} fontSize="7.5" textAnchor="end" fill="var(--neutral-3)">{t(p + "ax2")}</text>
          </svg>
          <div className="tx-graph-legend">
            <span>{t(p + "lg1")}</span>
          </div>
        </div>
      </IvCard>
      <IvCard label={t("viz.ind.signals")}>
        <IvSignals items={[
          ["flag", t(p + "s1")], ["flag", t(p + "s2")], ["flag", t(p + "s3")], ["ok", t(p + "s4")]]} />
      </IvCard>
      <IvDecision score={80} aml pill={t(p + "pill")} pillKind="alert"
        rows={[[t(p + "d1l"), t(p + "d1v")], [t(p + "d2l"), t(p + "d2v")], [t("viz.ind.case"), "CSE-8MDV1C"]]} />
    </IvShell>);
}

// ---------- Bancos digitales — account takeover en secuencia ----------
function IndDigitalBankVisual() {
  const { t } = useLang();
  const p = "viz.ibnk.";
  return (
    <IvShell p={p}>
      <IvCard label={t(p + "c1")}>
        <IvTimeline items={[
          ["14:02", t(p + "e1"), true],
          ["14:05", t(p + "e2"), true],
          ["14:09", t(p + "e3"), true],
          ["14:16", t(p + "e4"), true]]} />
      </IvCard>
      <IvCard label={t("viz.ind.signals")}>
        <IvSignals items={[
          ["flag", t(p + "s1")], ["flag", t(p + "s2")], ["flag", t(p + "s3")], ["ok", t(p + "s4")]]} />
      </IvCard>
      <IvDecision score={91} pill={t(p + "pill")} pillKind="alert"
        rows={[[t(p + "d1l"), t(p + "d1v")], [t(p + "d2l"), t(p + "d2v")], [t(p + "d3l"), t(p + "d3v")]]} />
    </IvShell>);
}

// ---------- Cripto — fondos de paso (fiat → cripto → retiro) ----------
function IndCryptoVisual() {
  const { t } = useLang();
  const p = "viz.icry.";
  const steps = [
    ["10:31", t(p + "st1"), t(p + "a1"), t(p + "u1")],
    ["10:34", t(p + "st2"), t(p + "a2"), "spot"],
    ["10:53", t(p + "st3"), t(p + "a3"), t(p + "u3")]];
  return (
    <IvShell p={p}>
      <IvCard label={t(p + "c1")}>
        <div className="iv-flow">
          {steps.map(([time, l, amt, sub], i) =>
            <div key={i} className={"iv-flow-step" + (i === 2 ? " flag" : "")}>
              <span className="iv-flow-time">{time}</span>
              <span className="iv-flow-l">{l}</span>
              <span className="iv-flow-amt">{amt}</span>
              <span className="iv-flow-sub">{sub}</span>
            </div>
          )}
        </div>
      </IvCard>
      <IvCard label={t("viz.ind.signals")}>
        <IvSignals items={[
          ["flag", t(p + "s1")], ["flag", t(p + "s2")], ["flag", t(p + "s3")], ["ok", t(p + "s4")]]} />
      </IvCard>
      <IvDecision score={83} pill={t(p + "pill")} pillKind="reject"
        rows={[[t(p + "d1l"), "22 min"], [t(p + "d2l"), t(p + "d2v")], [t("viz.ind.case"), "CSE-6QHN0E"]]} />
    </IvShell>);
}

window.FEAT_VISUALS = Object.assign(window.FEAT_VISUALS || {}, {
  "ind-wallet": IndWalletVisual,
  "ind-processor": IndProcessorVisual,
  "ind-marketplace": IndMarketplaceVisual,
  "ind-ecommerce": IndEcommerceVisual,
  "ind-lending": IndLendingVisual,
  "ind-gaming": IndGamingVisual,
  "ind-gambling": IndGamblingVisual,
  "ind-remittance": IndRemittanceVisual,
  "ind-digitalbank": IndDigitalBankVisual,
  "ind-crypto": IndCryptoVisual
});
