// VISUAL EDITOR v2 — Seed data for 20 opportunities (VE1-VE20)
// All data modelled against MSJ-Opposition-Workflow v14 as reference artifact
(function () {
  const seed = {
    // ── VE1-VE5 Canvas Intelligence ─────────────────────────────────────────
    validationIssues: [
      { id: 'VI-001', step: 'step-4',  severity: 'error',   rule: 'integration.timeout.missing',       title: 'Cite-check has no timeout configured',       autofix: true },
      { id: 'VI-002', step: 'step-6a', severity: 'warning', rule: 'approval.role.undefined',           title: 'Client review has no defined role guardrail', autofix: false },
      { id: 'VI-003', step: 'step-7',  severity: 'error',   rule: 'loop.no-exit-condition',            title: 'Revisions loop lacks bounded exit',          autofix: true },
      { id: 'VI-004', step: 'step-8',  severity: 'warning', rule: 'integration.retry.not-idempotent',  title: 'PACER filing retry is not idempotent',        autofix: true },
      { id: 'VI-005', step: 'edge:step-5-step-6b', severity: 'info', rule: 'branch.coverage.low',      title: 'Auto-approve branch seen in 2% of runs',     autofix: false },
    ],
    versions: [
      { id: 'v14', label: 'v14 · current',    author: 'R. Patel',       ts: '2026-04-20 14:22', changes: 7,  note: 'Added auto-approve branch.' },
      { id: 'v13', label: 'v13',              author: 'R. Patel',       ts: '2026-04-18 09:50', changes: 3,  note: 'Increased cite-check retries.' },
      { id: 'v12', label: 'v12',              author: 'M. Chen',        ts: '2026-04-15 11:05', changes: 12, note: 'Split filing + service.' },
      { id: 'v11', label: 'v11 · tag: stable',author: 'A. Werner',      ts: '2026-04-07 16:40', changes: 2,  note: 'Stabilize before Q2 rollout.' },
      { id: 'v10', label: 'v10',              author: 'M. Chen',        ts: '2026-03-30 10:15', changes: 9,  note: 'Mass rebind of client fields.' },
    ],
    subflows: [
      { id: 'SF-discovery',  name: 'Discovery Intake',  nodes: 6, runs30d: 142, ownedBy: 'Litigation ops' },
      { id: 'SF-billing',    name: 'Retainer Check',    nodes: 3, runs30d: 310, ownedBy: 'Finance' },
      { id: 'SF-privlog',    name: 'Privilege Tagging', nodes: 8, runs30d: 98,  ownedBy: 'Litigation ops' },
      { id: 'SF-pacer',      name: 'PACER Publisher',   nodes: 4, runs30d: 77,  ownedBy: 'Court liaison' },
    ],
    primitives: [
      { id: 'PR-retry',    name: 'Retry with Backoff',   kind: 'wrapper',  usedIn: 14, config: '5x · exp · 2s base' },
      { id: 'PR-forEach',  name: 'For-Each (parallel)',  kind: 'iterator', usedIn: 6,  config: 'concurrency: 4' },
      { id: 'PR-parallel', name: 'Parallel Fan-out',     kind: 'structural', usedIn: 9, config: '3 branches · join: all' },
      { id: 'PR-timeout',  name: 'Timeout / Circuit',    kind: 'wrapper',  usedIn: 11, config: 'T=30s · open 60s' },
    ],
    swimlanes: [
      { id: 'lane-system',  label: 'SCRIPTS / SYSTEM', color: '#65A30D', stepCount: 1 },
      { id: 'lane-human',   label: 'DRAFTING TEAM',    color: '#1D4ED8', stepCount: 4 },
      { id: 'lane-partner', label: 'PARTNER',          color: '#B45309', stepCount: 1 },
      { id: 'lane-client',  label: 'CLIENT',           color: '#7C3AED', stepCount: 1 },
      { id: 'lane-court',   label: 'COURT SYSTEMS',    color: '#0D9488', stepCount: 2 },
    ],
    // ── VE6-VE8 Data & Testing ──────────────────────────────────────────────
    variables: [
      { id: 'var-matterId',       type: 'string', source: 'input',           producers: [],               consumers: ['step-1','step-4','step-8'], unused: false },
      { id: 'var-memoText',       type: 'markdown', source: 'step-1.output', producers: ['step-1'],       consumers: ['step-2','step-3'],          unused: false },
      { id: 'var-briefDraft',     type: 'document', source: 'step-3.output', producers: ['step-3'],       consumers: ['step-4','step-5','step-7'], unused: false },
      { id: 'var-citeIssues',     type: 'array<CitationIssue>', source: 'step-4.output', producers: ['step-4'], consumers: ['step-7'],             unused: false },
      { id: 'var-partnerVerdict', type: 'enum<approve|reject>', source: 'step-5.output', producers: ['step-5'], consumers: ['step-6a','step-6b'],  unused: false },
      { id: 'var-clientNotes',    type: 'string', source: 'step-6a.output',  producers: ['step-6a'],      consumers: ['step-7'],                  unused: false },
      { id: 'var-filingId',       type: 'string', source: 'step-8.output',   producers: ['step-8'],       consumers: ['step-9'],                  unused: false },
      { id: 'var-legacyTag',      type: 'string', source: 'context',         producers: [],               consumers: [],                          unused: true  },
    ],
    schemas: [
      { id: 'SCH-001', event: 'workflow.citation.checked',   version: '2.1', fieldCount: 14, required: 9,  deprecated: 1, bound: true  },
      { id: 'SCH-002', event: 'workflow.partner.verdict',    version: '1.4', fieldCount: 7,  required: 5,  deprecated: 0, bound: true  },
      { id: 'SCH-003', event: 'workflow.filing.submitted',   version: '3.0', fieldCount: 22, required: 14, deprecated: 3, bound: false },
      { id: 'SCH-004', event: 'workflow.service.delivered',  version: '1.2', fieldCount: 11, required: 8,  deprecated: 0, bound: true  },
    ],
    testFixtures: [
      { id: 'FX-happy',        name: 'Happy path',         lastRun: '2026-04-22 09:14', passed: true,  durMs: 22400, asserts: 18, failed: 0 },
      { id: 'FX-partner-rej',  name: 'Partner rejects',    lastRun: '2026-04-22 09:14', passed: true,  durMs: 14200, asserts: 12, failed: 0 },
      { id: 'FX-pacer-5xx',    name: 'PACER 5xx cascade',  lastRun: '2026-04-22 09:14', passed: false, durMs: 38900, asserts: 20, failed: 2 },
      { id: 'FX-auto-approve', name: 'Trivial rev branch', lastRun: '2026-04-22 09:14', passed: true,  durMs: 9100,  asserts: 8,  failed: 0 },
      { id: 'FX-priv-hit',     name: 'Privilege hit',      lastRun: '2026-04-21 17:02', passed: true,  durMs: 18700, asserts: 14, failed: 0 },
    ],
    // ── VE9-VE11 AI & Compliance ────────────────────────────────────────────
    copilotHistory: [
      { id: 'CH-12', ts: '2026-04-22 08:50', user: 'R. Patel',  prompt: 'Add a step to notify co-counsel on partner rejection.', status: 'applied',  nodesChanged: 2 },
      { id: 'CH-11', ts: '2026-04-21 15:33', user: 'M. Chen',   prompt: 'Make cite-check idempotent.',                            status: 'applied',  nodesChanged: 1 },
      { id: 'CH-10', ts: '2026-04-21 14:02', user: 'R. Patel',  prompt: 'Collapse memo + outline into one step.',                status: 'rejected', nodesChanged: 0 },
      { id: 'CH-09', ts: '2026-04-20 11:10', user: 'A. Werner', prompt: 'Route to SF-privlog before draft.',                     status: 'pending',  nodesChanged: 2 },
    ],
    optimizerSuggestions: [
      { id: 'OPT-1', rationale: 'Cite-check and draft are independent after outline — parallelize.', savingsMin: 18, confidence: 0.82, risk: 'low' },
      { id: 'OPT-2', rationale: 'Auto-approve branch rarely fires (2%); demote from parallel to conditional.', savingsMin: 3, confidence: 0.64, risk: 'low' },
      { id: 'OPT-3', rationale: 'Service retries cost $0.11/attempt — cap to 3.', savingsMin: 0, confidence: 0.93, risk: 'low' },
      { id: 'OPT-4', rationale: 'Partner review p95 = 14h; pre-route to senior counsel for pre-clear.', savingsMin: 410, confidence: 0.51, risk: 'medium' },
    ],
    complianceRules: [
      { id: 'RL-priv-tag',      rule: 'All outbound filings must pass privilege screen', bindings: ['step-4','step-8'], status: 'satisfied' },
      { id: 'RL-client-notify', rule: 'Client receives copy on filing',                  bindings: ['step-8'],           status: 'satisfied' },
      { id: 'RL-ndisa',         rule: 'NDIS-A: deposition transcripts signed within 30d', bindings: [],                  status: 'unbound'   },
      { id: 'RL-retain-7y',     rule: 'Retain filing artifacts 7y',                      bindings: ['step-8','step-9'],  status: 'satisfied' },
      { id: 'RL-pii-scrub',     rule: 'PII scrubbed from research memos',                bindings: ['step-1'],           status: 'warn'      },
    ],
    // ── VE12-VE15 Governance & Release ──────────────────────────────────────
    collaborators: [
      { id: 'U-rp', name: 'R. Patel',  role: 'Author',   cursor: { col: 3, row: 0 }, color: '#65A30D', online: true  },
      { id: 'U-mc', name: 'M. Chen',   role: 'Reviewer', cursor: { col: 5, row: 0 }, color: '#1D4ED8', online: true  },
      { id: 'U-aw', name: 'A. Werner', role: 'Observer', cursor: null,               color: '#7C3AED', online: false },
    ],
    comments: [
      { id: 'C-1', step: 'step-5', author: 'M. Chen',   ts: '2026-04-22 08:10', resolved: false, text: 'Add a fallback if partner is OOO > 48h.' },
      { id: 'C-2', step: 'step-4', author: 'A. Werner', ts: '2026-04-21 16:22', resolved: true,  text: 'Shepard only returns 25 per page; add pagination.' },
      { id: 'C-3', step: 'step-8', author: 'R. Patel',  ts: '2026-04-22 09:01', resolved: false, text: 'Can we parallelize PACER + service?' },
    ],
    approvalChain: [
      { id: 'AP-1', stage: 'Author sign-off',      role: 'Author',         approver: 'R. Patel',   status: 'approved', ts: '2026-04-22 09:00' },
      { id: 'AP-2', stage: 'Peer review',          role: 'Senior counsel', approver: 'M. Chen',    status: 'approved', ts: '2026-04-22 09:30' },
      { id: 'AP-3', stage: 'Compliance review',    role: 'Compliance',     approver: 'C. Ortega',  status: 'pending',  ts: null },
      { id: 'AP-4', stage: 'Ops sign-off',         role: 'Ops',            approver: 'A. Werner',  status: 'pending',  ts: null },
      { id: 'AP-5', stage: 'Publish (canary 10%)', role: 'Release mgr',    approver: 'System',     status: 'blocked',  ts: null },
    ],
    canary: {
      stage: 'canary-10',
      pctVariant: 10,
      runsBase: 842,
      runsVariant: 97,
      p95Base: 2340,
      p95Variant: 2120,
      successBase: 94.1,
      successVariant: 95.2,
      errRateBase: 0.031,
      errRateVariant: 0.022,
      healthGates: [
        { id: 'G1', label: 'p95 ≤ baseline + 10%', passing: true },
        { id: 'G2', label: 'success ≥ baseline - 0.5%', passing: true },
        { id: 'G3', label: 'err-rate ≤ 0.05', passing: true },
        { id: 'G4', label: '50 runs in canary window', passing: true },
      ],
      nextAutoStep: '25%',
      nextAutoIn: '4h 22m',
    },
    variants: [
      { id: 'VAR-01', name: 'Collapsed memo+outline',  allocation: 10, metric: 'p50 time', baseVal: '41m', variantVal: '34m', delta: '-17%', status: 'Canary' },
      { id: 'VAR-02', name: 'Pre-clear senior counsel', allocation: 5,  metric: 'p95 time', baseVal: '14h', variantVal: '9h',  delta: '-36%', status: 'Canary' },
    ],
    // ── VE16-VE18 Observability ─────────────────────────────────────────────
    heatmap: [
      { step: 'step-1', runs30d: 939, p50Ms: 4200,  p95Ms: 9100,  errRate: 0.005, lastRun: '2026-04-22 09:12' },
      { step: 'step-2', runs30d: 939, p50Ms: 32100, p95Ms: 54000, errRate: 0.002, lastRun: '2026-04-22 09:05' },
      { step: 'step-3', runs30d: 939, p50Ms: 95400, p95Ms: 182000,errRate: 0.004, lastRun: '2026-04-22 08:52' },
      { step: 'step-4', runs30d: 939, p50Ms: 7800,  p95Ms: 22000, errRate: 0.041, lastRun: '2026-04-22 09:18' },
      { step: 'step-5', runs30d: 920, p50Ms: 53000, p95Ms: 502000,errRate: 0.001, lastRun: '2026-04-22 09:00' },
      { step: 'step-6a',runs30d: 712, p50Ms: 71000, p95Ms: 240000,errRate: 0.003, lastRun: '2026-04-22 07:12' },
      { step: 'step-6b',runs30d: 19,  p50Ms: 1100,  p95Ms: 2400,  errRate: 0.000, lastRun: '2026-04-20 11:05' },
      { step: 'step-7', runs30d: 664, p50Ms: 44000, p95Ms: 120000,errRate: 0.007, lastRun: '2026-04-22 06:40' },
      { step: 'step-8', runs30d: 904, p50Ms: 18000, p95Ms: 62000, errRate: 0.018, lastRun: '2026-04-22 09:20' },
      { step: 'step-9', runs30d: 904, p50Ms: 4700,  p95Ms: 11200, errRate: 0.009, lastRun: '2026-04-22 09:22' },
    ],
    incidents: [
      { id: 'INC-204', ts: '2026-04-21 14:22', step: 'step-8', kind: 'PACER 503', runId: 'R-19441', severity: 'P2', mttrMin: 18 },
      { id: 'INC-203', ts: '2026-04-20 09:47', step: 'step-4', kind: 'Shepard timeout', runId: 'R-19402', severity: 'P3', mttrMin: 4 },
      { id: 'INC-202', ts: '2026-04-18 16:10', step: 'step-5', kind: 'Partner SLA breach', runId: 'R-19332', severity: 'P2', mttrMin: 240 },
      { id: 'INC-201', ts: '2026-04-16 11:00', step: 'step-9', kind: 'Service to wrong addr', runId: 'R-19281', severity: 'P1', mttrMin: 62 },
    ],
    costLedger: {
      period: 'April 2026 (MTD)',
      totalUsd: 4820.44,
      byStep: [
        { step: 'step-1', kind: 'LLM tokens', usd: 612.18 },
        { step: 'step-4', kind: 'Westlaw API', usd: 1420.00 },
        { step: 'step-8', kind: 'PACER filing', usd: 1880.00 },
        { step: 'step-9', kind: 'Service fees', usd: 780.26 },
        { step: 'compute', kind: 'Runtime', usd: 128.00 },
      ],
      budgetUsd: 6000,
      burnPct: 80.3,
      trajectory: 'under',
    },
    // ── VE19-VE20 Extensibility ─────────────────────────────────────────────
    marketplaceDropIns: [
      { id: 'DI-01', name: 'DocuSign envelope',   vendor: 'DocuSign',       kind: 'integration', category: 'E-sign',     verified: true,  installs: 1420 },
      { id: 'DI-02', name: 'Relativity migrate',  vendor: 'Relativity',     kind: 'integration', category: 'Ediscovery', verified: true,  installs: 812  },
      { id: 'DI-03', name: 'Outlook 365 inbox',   vendor: 'Microsoft',      kind: 'trigger',     category: 'Email',      verified: true,  installs: 3140 },
      { id: 'DI-04', name: 'Slack incident ping', vendor: 'Slack',          kind: 'notifier',    category: 'Chat',       verified: true,  installs: 980  },
      { id: 'DI-05', name: 'OpenAI GPT step',     vendor: 'OpenAI',         kind: 'script',      category: 'AI',         verified: true,  installs: 2110 },
      { id: 'DI-06', name: 'Internal bank KYC',   vendor: 'Acme Bank Ops',  kind: 'integration', category: 'Finance',    verified: false, installs: 41   },
    ],
    keyboardShortcuts: [
      { combo: 'N',         action: 'Add node',                 category: 'Canvas' },
      { combo: 'Shift+E',   action: 'Connect selected',         category: 'Canvas' },
      { combo: '/',         action: 'Open copilot',             category: 'AI' },
      { combo: 'Cmd+K',     action: 'Command palette',          category: 'Global' },
      { combo: 'Cmd+Z',     action: 'Undo',                     category: 'Edit' },
      { combo: 'V',         action: 'Run validation',           category: 'Validate' },
      { combo: 'T',         action: 'Run test fixtures',        category: 'Test' },
      { combo: 'P',         action: 'Request publish',          category: 'Release' },
    ],
    a11y: {
      issues: [
        { id: 'AX-1', rule: 'focus-visible on node selection', severity: 'minor',    status: 'open'   },
        { id: 'AX-2', rule: 'ARIA-label on connector endpoints', severity: 'moderate', status: 'open' },
        { id: 'AX-3', rule: 'color contrast on disabled edges',  severity: 'minor',    status: 'fixed'},
      ],
      score: 92,
      keyboardCoverage: 0.94,
    },
  };

  const ve = {
    money: (n) => (n == null || isNaN(n)) ? '—' :
      n >= 1e6 ? `$${(n/1e6).toFixed(2)}M` :
      n >= 1e3 ? `$${(n/1e3).toFixed(1)}K` : `$${n.toFixed(2)}`,
    ms: (ms) => ms == null ? '—' :
      ms >= 60000 ? `${(ms/60000).toFixed(1)}m` :
      ms >= 1000 ? `${(ms/1000).toFixed(1)}s` : `${ms}ms`,
    pct: (x, total) => !total ? '0%' : `${Math.round(100*x/total)}%`,
    pctRaw: (f) => `${(f*100).toFixed(1)}%`,
    severityColor: (s) => {
      if (s === 'error' || s === 'P0' || s === 'P1') return { color: '#B91C1C', bg: 'rgba(185,28,28,0.08)' };
      if (s === 'warning' || s === 'moderate' || s === 'P2') return { color: '#B45309', bg: 'rgba(180,83,9,0.08)' };
      if (s === 'info' || s === 'minor' || s === 'P3') return { color: '#1D4ED8', bg: 'rgba(29,78,216,0.06)' };
      return { color: '#475569', bg: 'rgba(71,85,105,0.06)' };
    },
    statusColor: (s) => {
      if (s === 'approved' || s === 'applied' || s === 'satisfied' || s === 'fixed' || s === 'Promoted') return { color: '#059669', bg: 'rgba(5,150,105,0.08)' };
      if (s === 'pending' || s === 'open' || s === 'Canary' || s === 'warn') return { color: '#B45309', bg: 'rgba(180,83,9,0.08)' };
      if (s === 'blocked' || s === 'rejected' || s === 'unbound') return { color: '#B91C1C', bg: 'rgba(185,28,28,0.08)' };
      return { color: '#475569', bg: 'rgba(71,85,105,0.06)' };
    },
  };

  window.VISUAL_EDITOR_DATA = seed;
  window.ve = ve;
})();
