// Synthetic but believable data for the dashboard

// Deterministic pseudo-random so charts are stable
function mulberry32(seed) {
  return function () {
    seed |= 0; seed = (seed + 0x6D2B79F5) | 0;
    let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}

function smoothSeries(n, base, amp, seed, drift = 0) {
  const r = mulberry32(seed);
  const out = [];
  let v = base;
  for (let i = 0; i < n; i++) {
    const target = base + Math.sin(i / (n / 6)) * amp * 0.5 + (r() - 0.5) * amp;
    v = v * 0.85 + target * 0.15 + drift;
    out.push(+v.toFixed(2));
  }
  return out;
}

// Hourly data (last 24h, 48 points = 30min cadence)
const HOURLY_N = 48;
window.EDGE_DATA = {
  // current readings
  current: {
    aqi: 41,
    aqiLabel: 'Good',
    temperature: 22.4,
    humidity: 47.1,
    pressure: 1012.3,
    co2: 612,
    pm25: 8.4,
    pm10: 14.2,
    voc: 0.32,
    no2: 18,
    o3: 24,
    noise: 38,
  },
  deltas: {
    temperature: +0.4,
    humidity: -1.2,
    pressure: +0.6,
    co2: +24,
    pm25: -1.6,
    pm10: -0.8,
    voc: 0,
    no2: +2,
    o3: -3,
    noise: -1,
  },
  // sparkline series (48 points)
  series: {
    temperature: smoothSeries(HOURLY_N, 22.4, 1.4, 11),
    humidity:    smoothSeries(HOURLY_N, 47, 5, 22),
    pressure:    smoothSeries(HOURLY_N, 1012.3, 1.2, 33),
    co2:         smoothSeries(HOURLY_N, 600, 80, 44),
    pm25:        smoothSeries(HOURLY_N, 9, 4, 55),
    pm10:        smoothSeries(HOURLY_N, 14, 5, 66),
    voc:         smoothSeries(HOURLY_N, 0.32, 0.08, 77),
    no2:         smoothSeries(HOURLY_N, 18, 6, 88),
    o3:          smoothSeries(HOURLY_N, 24, 5, 99),
    noise:       smoothSeries(HOURLY_N, 38, 8, 101),
  },
  // 7-day weekly trends (168 points, hourly)
  weekly: {
    temperature: smoothSeries(168, 22, 4, 200),
    humidity:    smoothSeries(168, 48, 10, 210),
    pressure:    smoothSeries(168, 1012, 6, 220),
    co2:         smoothSeries(168, 580, 140, 230),
    pm25:        smoothSeries(168, 11, 8, 240),
  },
  // 24h forecast (next 24 hourly points)
  forecast: {
    aqi: smoothSeries(24, 44, 14, 300, 0.4).map(v => Math.max(8, v)),
  },
  zones: [
    { id: 'lab-a', name: 'Lab A · Cleanroom',   aqi: 28, status: 'good',    devices: 4, temp: 21.1, hum: 42 },
    { id: 'lab-b', name: 'Lab B · Wet Chem',    aqi: 64, status: 'caution', devices: 3, temp: 23.6, hum: 51 },
    { id: 'atrium', name: 'Main Atrium',        aqi: 41, status: 'good',    devices: 6, temp: 22.4, hum: 47, active: true },
    { id: 'cafe',  name: 'Café & Lounge',       aqi: 52, status: 'good',    devices: 2, temp: 24.0, hum: 49 },
    { id: 'park',  name: 'Rooftop Garden',      aqi: 19, status: 'good',    devices: 2, temp: 19.8, hum: 56 },
    { id: 'lot',   name: 'Loading Dock',        aqi: 88, status: 'poor',    devices: 2, temp: 25.2, hum: 44 },
  ],
  alerts: [
    { id: 1, level: 'warn', zone: 'Loading Dock', metric: 'PM2.5', detail: 'Spike to 42 µg/m³, returning to baseline', time: '11m ago' },
    { id: 2, level: 'info', zone: 'Lab B',        metric: 'CO₂',   detail: 'Ventilation cycle scheduled · 14:00',       time: '32m ago' },
    { id: 3, level: 'good', zone: 'Cleanroom',    metric: 'VOC',   detail: 'Sustained < 0.3 ppm for 6 hours',          time: '1h ago' },
    { id: 4, level: 'info', zone: 'Rooftop',      metric: 'Pollen',detail: 'Outdoor pollen index moderate (4 / 10)',    time: '2h ago' },
  ],
  devices: [
    { id: 'EG-0142', name: 'Edge node 0142', zone: 'Main Atrium', battery: 92, rssi: -54, fw: '2.4.1', status: 'online' },
    { id: 'EG-0143', name: 'Edge node 0143', zone: 'Lab A',        battery: 78, rssi: -61, fw: '2.4.1', status: 'online' },
    { id: 'EG-0149', name: 'Edge node 0149', zone: 'Lab B',        battery: 41, rssi: -72, fw: '2.3.9', status: 'updating' },
    { id: 'EG-0151', name: 'Edge node 0151', zone: 'Loading Dock', battery: 23, rssi: -78, fw: '2.4.1', status: 'low-bat' },
  ],
  // health & comfort indices
  indices: {
    comfort: 78,
    productivity: 84,
    breathability: 91,
  },
};

window.EDGE_FMT = {
  fmt: (v, d=1) => (v == null ? '—' : (typeof v === 'number' ? v.toFixed(d) : v)),
  signed: (v) => (v > 0 ? `+${v.toFixed(1)}` : v.toFixed(1)),
};
