/* ═══════════════════════════════════════════════════════════════════
   PerfData.jsx — Performance / CWV sample data + verdict logic +
   thresholds. v1.0.0 — ported verbatim from the Claude Design v2
   package (_CLAUDE-DESIGN/_2026-06-12-v2-SaaS-Redesign/perf-data.jsx).
   Illustrative numbers — the surface runs on this design sample
   dataset until the real CWV/uptime data path (CrUX/RUM per the build
   spec) lands; the SA-only launch gate covers that window.
   Load order: SECOND of the four Perf modules.
   Registers: window.WPSB.PerfData = { CWV_THRESHOLDS, cwvVerdict,
   siteVerdict, PERF_SITES, perfAverages, PERF_SPARKS, PERF_ENDPOINTS }.
   ═══════════════════════════════════════════════════════════════════ */
(function () {

// Google CWV thresholds. verdict(metric, value) → 'good' | 'ni' | 'poor'.
const CWV_THRESHOLDS = {
  lcp: { goodMax: 2.5, niMax: 4.0, scaleMax: 6, unit: 's',  target: '≤ 2.5s', labelGood: '2.5s', labelNi: '4.0s', name: 'LCP', full: 'Largest Contentful Paint' },
  inp: { goodMax: 200, niMax: 500, scaleMax: 800, unit: 'ms', target: '≤ 200ms', labelGood: '200', labelNi: '500', name: 'INP', full: 'Interaction to Next Paint' },
  cls: { goodMax: 0.1, niMax: 0.25, scaleMax: 0.4, unit: '', target: '≤ 0.1', labelGood: '0.10', labelNi: '0.25', name: 'CLS', full: 'Cumulative Layout Shift' },
};
function cwvVerdict(metric, value) {
  const t = CWV_THRESHOLDS[metric];
  if (value <= t.goodMax) return 'good';
  if (value <= t.niMax) return 'ni';
  return 'poor';
}
// overall site verdict = worst of the three
function siteVerdict(m) {
  const order = { good: 0, ni: 1, poor: 2 };
  const vs = ['lcp', 'inp', 'cls'].map(k => cwvVerdict(k, m[k]));
  return vs.reduce((w, v) => order[v] > order[w] ? v : w, 'good');
}

// 30-day uptime helper: mostly up, sprinkle degraded/down
function uptimeDays(seed, downAt = [], degradedAt = []) {
  return Array.from({ length: 30 }, (_, i) => downAt.includes(i) ? 'down' : degradedAt.includes(i) ? 'degraded' : 'up');
}

const PERF_SITES = [
  { site: 'agencysite.com',     mobile: { lcp: 2.1, inp: 180, cls: 0.06 }, desktop: { lcp: 1.4, inp: 90,  cls: 0.03 }, uptime: 100,  days: uptimeDays(1) },
  { site: 'clientstore.example', mobile: { lcp: 3.4, inp: 240, cls: 0.12 }, desktop: { lcp: 2.2, inp: 150, cls: 0.07 }, uptime: 99.94, days: uptimeDays(2, [], [17]) },
  { site: 'bloghub.example',     mobile: { lcp: 4.6, inp: 560, cls: 0.28 }, desktop: { lcp: 3.1, inp: 320, cls: 0.18 }, uptime: 99.7,  days: uptimeDays(3, [21], [20, 22]) },
  { site: 'portfolio.example',   mobile: { lcp: 1.8, inp: 120, cls: 0.04 }, desktop: { lcp: 1.1, inp: 70,  cls: 0.02 }, uptime: 100,  days: uptimeDays(4) },
  { site: 'shopfront.example',   mobile: { lcp: 3.0, inp: 310, cls: 0.09 }, desktop: { lcp: 2.0, inp: 190, cls: 0.05 }, uptime: 99.99, days: uptimeDays(5) },
];

// avg across sites for the stat row (per device)
function perfAverages(device) {
  const n = PERF_SITES.length;
  const sum = PERF_SITES.reduce((a, s) => ({ lcp: a.lcp + s[device].lcp, inp: a.inp + s[device].inp, cls: a.cls + s[device].cls, up: a.up + s.uptime }), { lcp: 0, inp: 0, cls: 0, up: 0 });
  return { lcp: +(sum.lcp / n).toFixed(1), inp: Math.round(sum.inp / n), cls: +(sum.cls / n).toFixed(2), uptime: +(sum.up / n).toFixed(2) };
}

const PERF_SPARKS = {
  lcp: [2.8, 2.7, 2.6, 2.5, 2.4, 2.5, 2.4],
  inp: [240, 230, 220, 210, 205, 200, 195],
  cls: [0.14, 0.12, 0.11, 0.10, 0.09, 0.10, 0.09],
  uptime: [99.8, 99.9, 100, 99.95, 100, 99.99, 99.98],
};

const PERF_ENDPOINTS = [
  { path: '/shop/checkout', ms: 4200, reason: 'Unoptimized JS', verdict: 'poor' },
  { path: '/blog/2026-recap', ms: 3600, reason: 'Large hero image', verdict: 'ni' },
  { path: '/products/all', ms: 3100, reason: 'No caching', verdict: 'ni' },
  { path: '/account', ms: 2400, reason: 'Render-blocking CSS', verdict: 'ni' },
  { path: '/', ms: 1700, reason: 'Within budget', verdict: 'good' },
];

window.WPSB = window.WPSB || {};
window.WPSB.PerfData = { CWV_THRESHOLDS, cwvVerdict, siteVerdict, PERF_SITES, perfAverages, PERF_SPARKS, PERF_ENDPOINTS };
console.log('[WPSB] PerfData v1.0.0 loaded (sample dataset)');
})();
