/* global React, Icons */
// Mock data + shared helpers
const { useState, useEffect, useMemo, useRef, createElement: h } = React;

// -------- Mock Data --------
const PATIENTS = [
  { id: 'P-1042', name: 'أحمد محمد السيد', age: 34, gender: 'ذكر', phone: '+20 100 123 4567', email: 'ahmed.m@example.com', lastVisit: '2026-04-12', nextVisit: '2026-04-22', balance: 1250, status: 'active', insurance: 'مصر للتأمين الصحي', doctor: 'د. سارة', notes: 'حساسية من البنسلين', avatar: 'أح' },
  { id: 'P-1043', name: 'فاطمة علي حسن', age: 28, gender: 'أنثى', phone: '+20 100 234 5678', email: 'fatma.ali@example.com', lastVisit: '2026-04-15', nextVisit: '2026-05-01', balance: 0, status: 'active', insurance: 'أكسا للتأمين', doctor: 'د. خالد', notes: 'تقويم أسنان شفاف — المتابعة شهرياً', avatar: 'فا' },
  { id: 'P-1044', name: 'محمد عبد الرحمن', age: 45, gender: 'ذكر', phone: '+20 101 345 6789', email: 'mohamed.ar@example.com', lastVisit: '2026-04-10', nextVisit: '2026-04-20', balance: 3400, status: 'active', insurance: 'بدون تأمين', doctor: 'د. سارة', notes: 'مريض ضغط — مراعاة الجرعات', avatar: 'مح' },
  { id: 'P-1045', name: 'نورهان إبراهيم', age: 22, gender: 'أنثى', phone: '+20 102 456 7890', email: 'nora.i@example.com', lastVisit: '2026-03-28', nextVisit: '2026-04-25', balance: 500, status: 'active', insurance: 'بوبا العربية', doctor: 'د. منى', notes: '', avatar: 'نو' },
  { id: 'P-1046', name: 'يوسف طارق', age: 9, gender: 'ذكر', phone: '+20 103 567 8901', email: 'tarek.y@example.com', lastVisit: '2026-04-14', nextVisit: '2026-04-28', balance: 0, status: 'active', insurance: 'الأهلي للتأمين', doctor: 'د. منى', notes: 'طفل — يحتاج تهدئة قبل الكشف', avatar: 'يو' },
  { id: 'P-1047', name: 'سلمى حسام', age: 31, gender: 'أنثى', phone: '+20 104 678 9012', email: 'salma.h@example.com', lastVisit: '2026-04-16', nextVisit: null, balance: 2100, status: 'pending', insurance: 'مصر للتأمين الصحي', doctor: 'د. خالد', notes: '', avatar: 'سل' },
  { id: 'P-1048', name: 'عمر شريف', age: 52, gender: 'ذكر', phone: '+20 105 789 0123', email: 'omar.s@example.com', lastVisit: '2026-02-20', nextVisit: null, balance: 0, status: 'inactive', insurance: 'بدون تأمين', doctor: 'د. سارة', notes: '', avatar: 'عم' },
  { id: 'P-1049', name: 'رنا أشرف', age: 38, gender: 'أنثى', phone: '+20 106 890 1234', email: 'rana.a@example.com', lastVisit: '2026-04-17', nextVisit: '2026-04-24', balance: 750, status: 'active', insurance: 'بوبا العربية', doctor: 'د. منى', notes: '', avatar: 'رن' },
];

const DOCTORS = [
  { id: 'D1', name: 'د. سارة عبد العزيز', specialty: 'طب أسنان تجميلي', color: '#0891b2', avatar: 'سا', status: 'متاحة', patients: 128, rating: 4.9 },
  { id: 'D2', name: 'د. خالد منصور', specialty: 'جراحة فم ووجه وفكين', color: '#7c3aed', avatar: 'خا', status: 'مع مريض', patients: 94, rating: 4.8 },
  { id: 'D3', name: 'د. منى فؤاد', specialty: 'تقويم أسنان', color: '#db2777', avatar: 'من', status: 'متاحة', patients: 156, rating: 4.9 },
  { id: 'D4', name: 'د. يوسف حامد', specialty: 'طب أسنان أطفال', color: '#059669', avatar: 'يو', status: 'في إجازة', patients: 71, rating: 4.7 },
];

const STAFF = [
  { id: 'S1', name: 'ندى صالح', role: 'ممرضة أسنان', phone: '+20 111 222 3333', shift: 'صباحي', avatar: 'ند' },
  { id: 'S2', name: 'كريم سعد', role: 'مساعد طبيب', phone: '+20 112 333 4444', shift: 'مسائي', avatar: 'كر' },
  { id: 'S3', name: 'هدى عماد', role: 'سكرتيرة', phone: '+20 113 444 5555', shift: 'صباحي', avatar: 'هد' },
  { id: 'S4', name: 'طارق فهمي', role: 'محاسب', phone: '+20 114 555 6666', shift: 'صباحي', avatar: 'طا' },
];

const APPOINTMENTS = [
  { id: 'A1', patient: 'أحمد محمد السيد', patientId: 'P-1042', doctor: 'د. سارة', time: '09:00', duration: 30, date: '2026-04-18', type: 'تنظيف وتلميع', status: 'confirmed', room: 'غرفة 1' },
  { id: 'A2', patient: 'فاطمة علي حسن', patientId: 'P-1043', doctor: 'د. منى', time: '09:30', duration: 45, date: '2026-04-18', type: 'متابعة تقويم', status: 'confirmed', room: 'غرفة 2' },
  { id: 'A3', patient: 'محمد عبد الرحمن', patientId: 'P-1044', doctor: 'د. سارة', time: '10:30', duration: 60, date: '2026-04-18', type: 'حشو عصب', status: 'in-progress', room: 'غرفة 1' },
  { id: 'A4', patient: 'نورهان إبراهيم', patientId: 'P-1045', doctor: 'د. خالد', time: '11:30', duration: 30, date: '2026-04-18', type: 'كشف', status: 'pending', room: 'غرفة 3' },
  { id: 'A5', patient: 'يوسف طارق', patientId: 'P-1046', doctor: 'د. منى', time: '12:00', duration: 45, date: '2026-04-18', type: 'خلع لبني', status: 'confirmed', room: 'غرفة 2' },
  { id: 'A6', patient: 'سلمى حسام', patientId: 'P-1047', doctor: 'د. خالد', time: '13:30', duration: 60, date: '2026-04-18', type: 'تركيب تاج', status: 'confirmed', room: 'غرفة 3' },
  { id: 'A7', patient: 'رنا أشرف', patientId: 'P-1049', doctor: 'د. سارة', time: '15:00', duration: 30, date: '2026-04-18', type: 'كشف دوري', status: 'confirmed', room: 'غرفة 1' },
  { id: 'A8', patient: 'عمر شريف', patientId: 'P-1048', doctor: 'د. سارة', time: '16:00', duration: 45, date: '2026-04-18', type: 'حشو', status: 'pending', room: 'غرفة 1' },
];

const INVENTORY = [
  { id: 'I1', name: 'مخدر موضعي Lidocaine 2%', category: 'أدوية', stock: 45, minStock: 20, unit: 'أمبول', price: 18, supplier: 'فارما مصر', expiry: '2026-09-15' },
  { id: 'I2', name: 'قفازات لاتكس معقمة', category: 'مستلزمات', stock: 8, minStock: 30, unit: 'علبة', price: 85, supplier: 'ميديكال جلف', expiry: '2027-03-01' },
  { id: 'I3', name: 'حشو كومبوزيت A2', category: 'مواد حشو', stock: 22, minStock: 10, unit: 'محقن', price: 340, supplier: '3M Egypt', expiry: '2026-12-10' },
  { id: 'I4', name: 'أكياس تعقيم', category: 'مستلزمات', stock: 500, minStock: 200, unit: 'كيس', price: 2, supplier: 'فارما مصر', expiry: '2028-01-01' },
  { id: 'I5', name: 'سنون تقويم', category: 'تقويم', stock: 140, minStock: 50, unit: 'قطعة', price: 12, supplier: 'Ortho International', expiry: '—' },
  { id: 'I6', name: 'مضمضة كلورهكسيدين', category: 'أدوية', stock: 4, minStock: 15, unit: 'زجاجة', price: 45, supplier: 'فارما مصر', expiry: '2026-06-30' },
  { id: 'I7', name: 'أشعة بانوراما فيلم', category: 'أشعة', stock: 28, minStock: 10, unit: 'فيلم', price: 75, supplier: 'Kodak Dental', expiry: '2027-02-14' },
  { id: 'I8', name: 'قناع طبي N95', category: 'مستلزمات', stock: 180, minStock: 100, unit: 'قطعة', price: 15, supplier: 'ميديكال جلف', expiry: '2028-05-20' },
];

const TREATMENT_TYPES = [
  { id: 'T1', name: 'كشف وفحص', price: 150, duration: 30, category: 'عام' },
  { id: 'T2', name: 'تنظيف وتلميع', price: 400, duration: 45, category: 'وقائي' },
  { id: 'T3', name: 'حشو كومبوزيت', price: 600, duration: 60, category: 'ترميمي' },
  { id: 'T4', name: 'حشو عصب', price: 2500, duration: 90, category: 'علاجي' },
  { id: 'T5', name: 'خلع سن', price: 500, duration: 45, category: 'جراحي' },
  { id: 'T6', name: 'خلع جراحي', price: 1500, duration: 60, category: 'جراحي' },
  { id: 'T7', name: 'تاج زيركون', price: 4500, duration: 120, category: 'تركيبات' },
  { id: 'T8', name: 'تبييض أسنان', price: 3000, duration: 90, category: 'تجميلي' },
  { id: 'T9', name: 'زراعة سن', price: 12000, duration: 120, category: 'جراحي' },
  { id: 'T10', name: 'تقويم شفاف', price: 35000, duration: 60, category: 'تقويم' },
];

const INVOICES = [
  { id: 'INV-2026-0142', patient: 'أحمد محمد السيد', patientId: 'P-1042', date: '2026-04-12', total: 2500, paid: 1250, status: 'partial', items: [{ name: 'حشو عصب', qty: 1, price: 2500 }] },
  { id: 'INV-2026-0143', patient: 'فاطمة علي حسن', patientId: 'P-1043', date: '2026-04-15', total: 1500, paid: 1500, status: 'paid', items: [{ name: 'متابعة تقويم', qty: 1, price: 1500 }] },
  { id: 'INV-2026-0144', patient: 'محمد عبد الرحمن', patientId: 'P-1044', date: '2026-04-10', total: 4500, paid: 1100, status: 'partial', items: [{ name: 'تاج زيركون', qty: 1, price: 4500 }] },
  { id: 'INV-2026-0145', patient: 'نورهان إبراهيم', patientId: 'P-1045', date: '2026-03-28', total: 500, paid: 0, status: 'unpaid', items: [{ name: 'كشف', qty: 1, price: 150 }, { name: 'أشعة', qty: 1, price: 350 }] },
  { id: 'INV-2026-0146', patient: 'يوسف طارق', patientId: 'P-1046', date: '2026-04-14', total: 800, paid: 800, status: 'paid', items: [{ name: 'خلع لبني', qty: 2, price: 400 }] },
  { id: 'INV-2026-0147', patient: 'سلمى حسام', patientId: 'P-1047', date: '2026-04-16', total: 2100, paid: 0, status: 'unpaid', items: [{ name: 'تركيب تاج', qty: 1, price: 2100 }] },
];

const PRESCRIPTIONS = [
  { id: 'RX-001', patient: 'أحمد محمد السيد', date: '2026-04-12', doctor: 'د. سارة', meds: [{ name: 'أموكسيسيلين 500mg', dose: 'كبسولة كل 8 ساعات', duration: '7 أيام' }, { name: 'بروفين 400mg', dose: 'قرص كل 6 ساعات عند الألم', duration: '3 أيام' }] },
  { id: 'RX-002', patient: 'محمد عبد الرحمن', date: '2026-04-10', doctor: 'د. سارة', meds: [{ name: 'كلافوكس 1g', dose: 'قرص كل 12 ساعة', duration: '5 أيام' }, { name: 'مضمضة كلورهكسيدين', dose: 'مرتان يومياً بعد الأكل', duration: '10 أيام' }] },
  { id: 'RX-003', patient: 'سلمى حسام', date: '2026-04-16', doctor: 'د. خالد', meds: [{ name: 'كيتولاك 10mg', dose: 'قرص عند الألم', duration: 'حسب الحاجة' }] },
];

const MESSAGES = [
  { id: 'M1', from: 'د. سارة عبد العزيز', avatar: 'سا', preview: 'هل أشعة المريض أحمد جاهزة؟', time: '10:24', unread: 2, online: true },
  { id: 'M2', from: 'هدى عماد (استقبال)', avatar: 'هد', preview: 'تم تأكيد مواعيد الغد', time: '09:58', unread: 0, online: true },
  { id: 'M3', from: 'د. خالد منصور', avatar: 'خا', preview: 'أحتاج تحديث قائمة الأدوات الجراحية', time: 'أمس', unread: 1, online: false },
  { id: 'M4', from: 'كريم سعد', avatar: 'كر', preview: 'جهاز التعقيم يحتاج صيانة', time: 'أمس', unread: 0, online: true },
  { id: 'M5', from: 'طارق فهمي (محاسبة)', avatar: 'طا', preview: 'تقرير الإيرادات الأسبوعي', time: 'السبت', unread: 0, online: false },
];

const NOTIFICATIONS = [
  { id: 'N1', type: 'warning', text: 'مخزون القفازات منخفض (8 علب فقط)', time: 'منذ 5 دقائق' },
  { id: 'N2', type: 'info', text: 'موعد جديد: رنا أشرف الساعة 3:00', time: 'منذ 15 دقيقة' },
  { id: 'N3', type: 'success', text: 'تم سداد فاتورة #INV-0143', time: 'منذ ساعة' },
  { id: 'N4', type: 'warning', text: 'مضمضة كلورهكسيدين تنتهي خلال شهرين', time: 'اليوم' },
];

const TOOTH_NUMBERS_UPPER = [18, 17, 16, 15, 14, 13, 12, 11, 21, 22, 23, 24, 25, 26, 27, 28];
const TOOTH_NUMBERS_LOWER = [48, 47, 46, 45, 44, 43, 42, 41, 31, 32, 33, 34, 35, 36, 37, 38];

// Tooth initial states (by number)
const TOOTH_STATES = {
  16: 'cavity', 26: 'filled', 36: 'crown', 46: 'filled',
  11: 'treated', 21: 'treated', 48: 'missing', 38: 'missing',
};

// ------- EGP formatter -------
const fmtEGP = (n) => new Intl.NumberFormat('ar-EG').format(n) + ' ج.م';
const fmtNum = (n) => new Intl.NumberFormat('ar-EG').format(n);

// Format Arabic date
const fmtDate = (dateStr) => {
  try {
    const d = new Date(dateStr);
    return d.toLocaleDateString('ar-EG', { day: 'numeric', month: 'short', year: 'numeric' });
  } catch { return dateStr; }
};

// -------- Global context --------
const AppContext = React.createContext(null);

Object.assign(window, {
  PATIENTS, DOCTORS, STAFF, APPOINTMENTS, INVENTORY, TREATMENT_TYPES,
  INVOICES, PRESCRIPTIONS, MESSAGES, NOTIFICATIONS,
  TOOTH_NUMBERS_UPPER, TOOTH_NUMBERS_LOWER, TOOTH_STATES,
  fmtEGP, fmtNum, fmtDate,
  AppContext,
});
