// Hospital Dashboard — mock data
// Realistic ward layout, devices, patients, vitals streams.

const HOSP_HOSPITALS = [
  { id: 1, name: 'Apollo General Hospital', code: 'APO-MUM', city: 'Mumbai · Maharashtra', wards: 4, beds: 28, patients: 24, alerts: 3, healthy: 21, watchlist: 3, devices: 36 },
  { id: 2, name: 'AIIMS Peripheral Centre', code: 'AII-PUN', city: 'Pune · Maharashtra', wards: 3, beds: 18, patients: 16, alerts: 0, healthy: 16, watchlist: 0, devices: 24 },
  { id: 3, name: 'Fortis City Clinic', code: 'FCC-BLR', city: 'Bangalore · Karnataka', wards: 2, beds: 12, patients: 9, alerts: 1, healthy: 8, watchlist: 1, devices: 14 },
  { id: 4, name: 'Manipal Med-Center', code: 'MAN-DEL', city: 'New Delhi · NCR', wards: 5, beds: 36, patients: 32, alerts: 2, healthy: 28, watchlist: 4, devices: 48 },
];

const HOSP_WARDS = [
  { id: 'icu', name: 'ICU', color: '#ef4444', x: 6, y: 8, w: 44, h: 36 },
  { id: 'gen', name: 'General', color: '#6366f1', x: 52, y: 8, w: 42, h: 36 },
  { id: 'car', name: 'Cardiac', color: '#f59e0b', x: 6, y: 46, w: 36, h: 32 },
  { id: 'rec', name: 'Recovery', color: '#10b981', x: 44, y: 46, w: 50, h: 32 },
];

// 12-bed ward grid
const HOSP_BEDS = [
  // ICU
  { id: 1, name: 'Bed 04', ward: 'icu', patient: 'Rahul Sharma', mrn: 'PT-1-00001', age: 58, sex: 'M', device: 'Monitor-A1', status: 'alert', baseHR: 132, alertText: 'HR sustained above 120bpm' },
  { id: 2, name: 'Bed 05', ward: 'icu', patient: 'Priya Nair', mrn: 'PT-1-00002', age: 42, sex: 'F', device: 'Monitor-A2', status: 'watch', baseHR: 102 },
  { id: 3, name: 'Bed 06', ward: 'icu', patient: 'Arjun Singh', mrn: 'PT-1-00005', age: 64, sex: 'M', device: 'Monitor-A5', status: 'stable', baseHR: 78 },
  { id: 4, name: 'Bed 07', ward: 'icu', patient: 'Lakshmi Iyer', mrn: 'PT-1-00007', age: 55, sex: 'F', device: 'Monitor-A8', status: 'stable', baseHR: 82 },
  // General
  { id: 5, name: 'Bed 12', ward: 'gen', patient: 'Suresh Patel', mrn: 'PT-1-00003', age: 47, sex: 'M', device: 'Monitor-A3', status: 'stable', baseHR: 74 },
  { id: 6, name: 'Bed 13', ward: 'gen', patient: 'Kavya Reddy', mrn: 'PT-1-00004', age: 32, sex: 'F', device: 'Monitor-A4', status: 'stable', baseHR: 70 },
  { id: 7, name: 'Bed 14', ward: 'gen', patient: 'Vikram Shah', mrn: 'PT-1-00009', age: 51, sex: 'M', device: 'Monitor-A9', status: 'stable', baseHR: 76 },
  { id: 8, name: 'Bed 15', ward: 'gen', patient: '— Vacant —', mrn: null, age: null, sex: null, device: 'Monitor-A7', status: 'idle', baseHR: 0 },
  // Cardiac
  { id: 9, name: 'Bed 21', ward: 'car', patient: 'Meena Joshi', mrn: 'PT-1-00006', age: 68, sex: 'F', device: 'Monitor-A6', status: 'watch', baseHR: 96, alertText: 'BP trending high' },
  { id: 10, name: 'Bed 22', ward: 'car', patient: 'Rajesh Verma', mrn: 'PT-1-00010', age: 72, sex: 'M', device: 'Monitor-A10', status: 'stable', baseHR: 88 },
  // Recovery
  { id: 11, name: 'Bed 31', ward: 'rec', patient: 'Anjali Mehra', mrn: 'PT-1-00011', age: 38, sex: 'F', device: 'Monitor-A11', status: 'stable', baseHR: 72 },
  { id: 12, name: 'Bed 32', ward: 'rec', patient: 'Karthik Iyer', mrn: 'PT-1-00012', age: 29, sex: 'M', device: 'Monitor-A12', status: 'stable', baseHR: 66 },
];

const HOSP_ALERTS = [
  { id: 'a1', sev: 'critical', bed: 'Bed 04', patient: 'Rahul Sharma', vital: 'Heart Rate', value: '132 bpm', threshold: '> 120 bpm', time: '2 min ago', ack: false, note: 'Sustained tachycardia for 4 minutes' },
  { id: 'a2', sev: 'warning', bed: 'Bed 21', patient: 'Meena Joshi', vital: 'BP Systolic', value: '156 mmHg', threshold: '> 140 mmHg', time: '8 min ago', ack: false, note: 'Mild hypertension reading' },
  { id: 'a3', sev: 'warning', bed: 'Bed 05', patient: 'Priya Nair', vital: 'SpO₂', value: '94 %', threshold: '< 95 %', time: '14 min ago', ack: true, note: 'Borderline desaturation; resolved after positional change' },
  { id: 'a4', sev: 'critical', bed: 'Bed 09', patient: 'Sunita Kumar', vital: 'Heart Rate', value: '48 bpm', threshold: '< 60 bpm', time: '32 min ago', ack: true, note: 'Bradycardia; clinician notified at 09:14' },
  { id: 'a5', sev: 'warning', bed: 'Bed 11', patient: 'Anjali Mehra', vital: 'Temperature', value: '38.4 °C', threshold: '≥ 38.0 °C', time: '1 h 12 min ago', ack: true, note: 'Low-grade fever; Tylenol administered' },
  { id: 'a6', sev: 'info', bed: 'Bed 07', patient: 'Lakshmi Iyer', vital: 'Device offline', value: '—', threshold: '> 60s', time: '3 h ago', ack: true, note: 'Device reconnected automatically' },
];

const HOSP_PATIENTS = [
  { id: 'PT-1-00001', name: 'Rahul Sharma', age: 58, sex: 'M', bed: 'Bed 04', ward: 'ICU', admitted: '2026-05-15', condition: 'Post-MI observation', risk: 'high' },
  { id: 'PT-1-00002', name: 'Priya Nair', age: 42, sex: 'F', bed: 'Bed 05', ward: 'ICU', admitted: '2026-05-16', condition: 'Sepsis monitoring', risk: 'medium' },
  { id: 'PT-1-00003', name: 'Suresh Patel', age: 47, sex: 'M', bed: 'Bed 12', ward: 'General', admitted: '2026-05-14', condition: 'Recovery — knee replacement', risk: 'low' },
  { id: 'PT-1-00004', name: 'Kavya Reddy', age: 32, sex: 'F', bed: 'Bed 13', ward: 'General', admitted: '2026-05-17', condition: 'Pneumonia', risk: 'low' },
  { id: 'PT-1-00005', name: 'Arjun Singh', age: 64, sex: 'M', bed: 'Bed 06', ward: 'ICU', admitted: '2026-05-12', condition: 'Cardiac arrhythmia', risk: 'medium' },
  { id: 'PT-1-00006', name: 'Meena Joshi', age: 68, sex: 'F', bed: 'Bed 21', ward: 'Cardiac', admitted: '2026-05-11', condition: 'Hypertensive crisis', risk: 'medium' },
];

const HOSP_DEVICES = [
  { id: 1, name: 'Monitor-A1', mac: 'AA:BB:CC:DD:01:01', bed: 'Bed 04', patient: 'Rahul Sharma', status: 'online', release: 'v1.3.0', battery: 78, signal: 92 },
  { id: 2, name: 'Monitor-A2', mac: 'AA:BB:CC:DD:01:02', bed: 'Bed 05', patient: 'Priya Nair', status: 'online', release: 'v1.3.0', battery: 64, signal: 88 },
  { id: 3, name: 'Monitor-A3', mac: 'AA:BB:CC:DD:01:03', bed: 'Bed 12', patient: 'Suresh Patel', status: 'online', release: 'v1.2.4', battery: 92, signal: 95 },
  { id: 4, name: 'Monitor-A4', mac: 'AA:BB:CC:DD:01:04', bed: 'Bed 13', patient: 'Kavya Reddy', status: 'online', release: 'v1.3.0', battery: 41, signal: 76 },
  { id: 5, name: 'Monitor-A5', mac: 'AA:BB:CC:DD:01:05', bed: 'Bed 06', patient: 'Arjun Singh', status: 'online', release: 'v1.3.0', battery: 88, signal: 98 },
  { id: 6, name: 'Monitor-A6', mac: 'AA:BB:CC:DD:01:06', bed: 'Bed 21', patient: 'Meena Joshi', status: 'online', release: 'v1.2.4', battery: 55, signal: 82 },
  { id: 7, name: 'Monitor-A7', mac: 'AA:BB:CC:DD:01:07', bed: 'Bed 15', patient: '—', status: 'idle', release: 'v1.3.0', battery: 100, signal: 90 },
  { id: 8, name: 'Monitor-A8', mac: 'AA:BB:CC:DD:01:08', bed: 'Bed 07', patient: 'Lakshmi Iyer', status: 'online', release: 'v1.3.0', battery: 70, signal: 85 },
  { id: 9, name: 'Monitor-A9', mac: 'AA:BB:CC:DD:01:09', bed: 'Bed 14', patient: 'Vikram Shah', status: 'online', release: 'v1.3.0', battery: 33, signal: 79 },
  { id: 10, name: 'Monitor-A10', mac: 'AA:BB:CC:DD:01:0A', bed: 'Bed 22', patient: 'Rajesh Verma', status: 'online', release: 'v1.3.0', battery: 81, signal: 91 },
  { id: 11, name: 'Monitor-A11', mac: 'AA:BB:CC:DD:01:0B', bed: 'Bed 31', patient: 'Anjali Mehra', status: 'online', release: 'v1.3.0', battery: 67, signal: 94 },
  { id: 12, name: 'Monitor-A12', mac: 'AA:BB:CC:DD:01:0C', bed: 'Bed 32', patient: 'Karthik Iyer', status: 'online', release: 'v1.2.4', battery: 89, signal: 96 },
];

// Generate a smooth ECG-like waveform (one beat shape repeated with noise)
function ecgWaveform(samples, beats, amp, noise) {
  const out = new Array(samples);
  const period = samples / beats;
  for (let i = 0; i < samples; i++) {
    const t = (i % period) / period; // 0..1 within beat
    let v = 0;
    // P wave
    if (t < 0.10) v = Math.sin(t * Math.PI / 0.10) * 0.15;
    // QRS complex (sharp spike)
    else if (t < 0.13) v = -0.25 * ((t - 0.10) / 0.03);
    else if (t < 0.17) v = -0.25 + 1.25 * ((t - 0.13) / 0.04);
    else if (t < 0.21) v = 1.0 - 1.45 * ((t - 0.17) / 0.04);
    else if (t < 0.24) v = -0.45 + 0.45 * ((t - 0.21) / 0.03);
    // T wave
    else if (t < 0.50) v = Math.sin((t - 0.24) * Math.PI / 0.26) * 0.28;
    else v = 0;
    out[i] = v * amp + (Math.random() - 0.5) * noise;
  }
  return out;
}

Object.assign(window, {
  HOSP_HOSPITALS, HOSP_WARDS, HOSP_BEDS, HOSP_ALERTS, HOSP_PATIENTS, HOSP_DEVICES,
  ecgWaveform,
});
