const fmtIDR = (n) => 'Rp ' + (n || 0).toLocaleString('id-ID');
const fmtShort = (n) => {
  if (n >= 1e9) return 'Rp ' + (n / 1e9).toFixed(1) + 'M';
  if (n >= 1e6) return 'Rp ' + (n / 1e6).toFixed(1) + 'jt';
  if (n >= 1e3) return 'Rp ' + (n / 1e3).toFixed(0) + 'rb';
  return 'Rp ' + n;
};

const CUSTOMERS = [
  { id: 'c01', company: 'PT Sinar Retail Nusantara', contact: 'Bayu Saputra', role: 'Head of Procurement', city: 'Jakarta', segment: 'Enterprise', lifecycle: 'Customer', owner: 'Rina W.', lastContact: '2 jam lalu', nextFollow: 'Besok 10:00', deal: 480000000, score: 92, status: 'active', email: 'bayu@sinarretail.co.id', phone: '+62 812-3344-5567', tone: 'indigo' },
  { id: 'c02', company: 'Kopi Senja Group', contact: 'Mira Anggraini', role: 'Founder', city: 'Bandung', segment: 'SMB', lifecycle: 'Proposal', owner: 'Dimas P.', lastContact: 'Kemarin', nextFollow: 'Kamis 14:00', deal: 125000000, score: 78, status: 'hot', email: 'mira@kopisenja.id', phone: '+62 813-9988-7765', tone: 'blue' },
  { id: 'c03', company: 'Klinik Prima Medika', contact: 'dr. Anindya', role: 'Direktur', city: 'Surabaya', segment: 'High Value', lifecycle: 'Qualified', owner: 'Rina W.', lastContact: '3 hari lalu', nextFollow: 'Hari ini 16:00', deal: 320000000, score: 88, status: 'active', email: 'anindya@primamedika.id', phone: '+62 821-3344-1122', tone: 'cyan' },
  { id: 'c04', company: 'CV Maju Distribusi', contact: 'Hendro K.', role: 'Owner', city: 'Semarang', segment: 'SMB', lifecycle: 'Prospect', owner: 'Andini S.', lastContact: '5 hari lalu', nextFollow: 'Overdue', deal: 65000000, score: 54, status: 'overdue', email: 'hendro@majudistribusi.com', phone: '+62 856-7788-9900', tone: 'amber' },
  { id: 'c05', company: 'Yayasan Pelita Bangsa', contact: 'Ibu Sari Wulan', role: 'Manajer Operasional', city: 'Yogyakarta', segment: 'High Value', lifecycle: 'Customer', owner: 'Dimas P.', lastContact: 'Minggu lalu', nextFollow: 'Senin 09:00', deal: 210000000, score: 81, status: 'active', email: 'sari@pelitabangsa.org', phone: '+62 811-2233-4455', tone: 'green' },
  { id: 'c06', company: 'Hotel Aruna Bali', contact: 'I Made Wira', role: 'GM', city: 'Denpasar', segment: 'Enterprise', lifecycle: 'Negotiation', owner: 'Rina W.', lastContact: 'Hari ini', nextFollow: 'Jumat 11:00', deal: 750000000, score: 94, status: 'hot', email: 'wira@arunabali.com', phone: '+62 819-7766-5544', tone: 'pink' },
  { id: 'c07', company: 'Toko Laris Jaya', contact: 'Pak Wahyudi', role: 'Pemilik', city: 'Medan', segment: 'SMB', lifecycle: 'Customer', owner: 'Andini S.', lastContact: '4 hari lalu', nextFollow: 'Sabtu 10:00', deal: 45000000, score: 67, status: 'active', email: 'wahyudi@larisjaya.id', phone: '+62 822-1199-8877', tone: 'violet' },
  { id: 'c08', company: 'Studio Kriya Nusantara', contact: 'Lina Hadi', role: 'Creative Director', city: 'Jakarta', segment: 'SMB', lifecycle: 'Lead', owner: 'Dimas P.', lastContact: 'Hari ini', nextFollow: 'Belum diatur', deal: 35000000, score: 48, status: 'cold', email: 'lina@kriyanusantara.id', phone: '+62 877-3322-1144', tone: 'slate' },
  { id: 'c09', company: 'PT Bumi Hijau Energi', contact: 'Reza Mahendra', role: 'CFO', city: 'Jakarta', segment: 'Enterprise', lifecycle: 'Customer', owner: 'Rina W.', lastContact: '1 jam lalu', nextFollow: 'Minggu depan', deal: 1200000000, score: 96, status: 'active', email: 'reza@bumihijau.id', phone: '+62 815-4433-2211', tone: 'green' },
  { id: 'c10', company: 'Sekolah Cendekia Mulia', contact: 'Bu Ratna', role: 'Kepala Sekolah', city: 'Malang', segment: 'High Value', lifecycle: 'Retention', owner: 'Andini S.', lastContact: '2 hari lalu', nextFollow: 'Rabu 13:00', deal: 180000000, score: 84, status: 'active', email: 'ratna@cendekiamulia.sch.id', phone: '+62 813-2211-3344', tone: 'blue' },
];

const ACTIVITIES = [
  { type: 'call', who: 'Rina W.', target: 'PT Sinar Retail', desc: 'Discovery call · 28 menit', when: '2 jam lalu', tone: 'blue' },
  { type: 'email', who: 'Dimas P.', target: 'Kopi Senja Group', desc: 'Kirim proposal v2 · dibuka 3x', when: '3 jam lalu', tone: 'indigo' },
  { type: 'meeting', who: 'Rina W.', target: 'Hotel Aruna Bali', desc: 'Demo product · 4 attendees', when: 'Hari ini 10:00', tone: 'pink' },
  { type: 'wa', who: 'Andini S.', target: 'CV Maju Distribusi', desc: 'Follow-up pricing · belum dibalas', when: 'Kemarin', tone: 'green' },
  { type: 'note', who: 'Dimas P.', target: 'Klinik Prima Medika', desc: 'Tambah catatan kebutuhan integrasi', when: 'Kemarin', tone: 'amber' },
  { type: 'call', who: 'Rina W.', target: 'PT Bumi Hijau', desc: 'Negotiation call · 45 menit', when: '1 hari lalu', tone: 'blue' },
  { type: 'meeting', who: 'Andini S.', target: 'Sekolah Cendekia', desc: 'Onboarding kickoff', when: '2 hari lalu', tone: 'pink' },
];

const COMMUNICATIONS = [
  { id: 1, channel: 'email', from: 'Bayu Saputra', company: 'PT Sinar Retail', subject: 'Re: Proposal integrasi POS', preview: 'Halo Rina, kami sudah review proposalnya. Tim IT punya beberapa pertanyaan terkait modul…', when: '14:22', sentiment: 'positive', unread: true },
  { id: 2, channel: 'wa', from: 'Mira Anggraini', company: 'Kopi Senja Group', subject: 'WhatsApp', preview: 'Boleh minta breakdown harga per outlet ya? Saya mau presentasi ke board minggu ini.', when: '13:05', sentiment: 'neutral', unread: true },
  { id: 3, channel: 'call', from: 'dr. Anindya', company: 'Klinik Prima Medika', subject: 'Inbound call · 12 menit', preview: 'Diskusi kebutuhan rekam medis terintegrasi dan rencana go-live Q3.', when: '11:40', sentiment: 'positive', unread: false },
  { id: 4, channel: 'email', from: 'Hendro K.', company: 'CV Maju Distribusi', subject: 'Pertanyaan modul gudang', preview: 'Kami butuh penjelasan lebih detail soal multi-warehouse dan stock opname…', when: 'Kemarin', sentiment: 'neutral', unread: false },
  { id: 5, channel: 'meeting', from: 'I Made Wira', company: 'Hotel Aruna Bali', subject: 'Meeting notes · Demo session', preview: 'Tim hotel sangat tertarik dengan fitur reservation. Minta proposal dengan opsi tahunan.', when: 'Kemarin', sentiment: 'positive', unread: false },
  { id: 6, channel: 'wa', from: 'Pak Wahyudi', company: 'Toko Laris Jaya', subject: 'WhatsApp', preview: 'Mas, mau tanya. Untuk training kasir ada biaya tambahan ga ya?', when: '2 hari lalu', sentiment: 'neutral', unread: false },
];

const PIPELINE_COLS = [
  { id: 'lead', label: 'New Lead', tone: 'slate' },
  { id: 'contacted', label: 'Contacted', tone: 'blue' },
  { id: 'qualified', label: 'Qualified', tone: 'cyan' },
  { id: 'proposal', label: 'Proposal', tone: 'indigo' },
  { id: 'negotiation', label: 'Negotiation', tone: 'amber' },
  { id: 'won', label: 'Won', tone: 'green' },
  { id: 'lost', label: 'Lost', tone: 'red' },
];

const DEALS = [
  { id: 'd1', stage: 'lead', company: 'Studio Kriya Nusantara', value: 35000000, next: 'Discovery call', owner: 'DP', overdue: false },
  { id: 'd2', stage: 'lead', company: 'Apotek Sehat Bersama', value: 28000000, next: 'Send intro deck', owner: 'AS', overdue: false },
  { id: 'd3', stage: 'contacted', company: 'CV Maju Distribusi', value: 65000000, next: 'Follow-up email', owner: 'AS', overdue: true },
  { id: 'd4', stage: 'contacted', company: 'Bengkel Mitra Otomotif', value: 92000000, next: 'Schedule demo', owner: 'RW', overdue: false },
  { id: 'd5', stage: 'qualified', company: 'Klinik Prima Medika', value: 320000000, next: 'Technical review', owner: 'RW', overdue: false },
  { id: 'd6', stage: 'qualified', company: 'Yayasan Pelita Bangsa', value: 210000000, next: 'Stakeholder mtg', owner: 'DP', overdue: false },
  { id: 'd7', stage: 'proposal', company: 'Kopi Senja Group', value: 125000000, next: 'Proposal review', owner: 'DP', overdue: false },
  { id: 'd8', stage: 'proposal', company: 'PT Sinar Retail', value: 480000000, next: 'Pricing call', owner: 'RW', overdue: false },
  { id: 'd9', stage: 'negotiation', company: 'Hotel Aruna Bali', value: 750000000, next: 'Contract draft', owner: 'RW', overdue: false },
  { id: 'd10', stage: 'won', company: 'PT Bumi Hijau Energi', value: 1200000000, next: 'Kickoff onboarding', owner: 'RW', overdue: false },
  { id: 'd11', stage: 'won', company: 'Sekolah Cendekia Mulia', value: 180000000, next: 'Onboarding mgr', owner: 'AS', overdue: false },
  { id: 'd12', stage: 'lost', company: 'PT Indojaya Mandiri', value: 95000000, next: 'Re-engagement Q4', owner: 'DP', overdue: false },
];

const REMINDERS = [
  { id: 1, title: 'Call CFO PT Sinar Retail untuk konfirmasi PO', who: 'Bayu Saputra', when: 'Hari ini · 10:00', tone: 'red', tag: 'Overdue 2 jam' },
  { id: 2, title: 'Follow-up proposal Kopi Senja Group', who: 'Mira Anggraini', when: 'Hari ini · 14:00', tone: 'amber', tag: 'Hari ini' },
  { id: 3, title: 'Demo lanjutan ke tim Klinik Prima Medika', who: 'dr. Anindya', when: 'Besok · 09:30', tone: 'blue', tag: 'Besok' },
  { id: 4, title: 'Kirim kontrak final Hotel Aruna Bali', who: 'I Made Wira', when: 'Jumat · 11:00', tone: 'indigo', tag: '3 hari' },
  { id: 5, title: 'Quarterly review meeting PT Bumi Hijau', who: 'Reza Mahendra', when: 'Senin depan · 13:30', tone: 'green', tag: '5 hari' },
  { id: 6, title: 'Welcome call onboarding Sekolah Cendekia', who: 'Bu Ratna', when: 'Rabu · 13:00', tone: 'cyan', tag: '7 hari' },
];

const SEGMENTS = [
  { name: 'Enterprise', count: 18, revenue: 4800000000, growth: 12, color: 'indigo', desc: 'Klien korporat > 500 karyawan' },
  { name: 'SMB', count: 124, revenue: 1900000000, growth: 8, color: 'blue', desc: 'Usaha menengah & kecil' },
  { name: 'High Value', count: 32, revenue: 3200000000, growth: 15, color: 'cyan', desc: 'Lifetime value > Rp 500jt' },
  { name: 'At Risk', count: 14, revenue: 480000000, growth: -22, color: 'amber', desc: 'Tidak ada aktivitas 30+ hari' },
  { name: 'Dormant', count: 41, revenue: 0, growth: -5, color: 'slate', desc: 'Customer tidak aktif > 90 hari' },
  { name: 'New Leads', count: 56, revenue: 0, growth: 34, color: 'green', desc: 'Lead masuk 14 hari terakhir' },
  { name: 'Repeat Buyers', count: 78, revenue: 2100000000, growth: 6, color: 'violet', desc: 'Beli > 2 kali' },
  { name: 'Support Heavy', count: 22, revenue: 540000000, growth: -3, color: 'pink', desc: 'Buka ticket > 5 kali/bulan' },
];

const TICKETS = [
  { id: 'TKT-1042', company: 'PT Sinar Retail', subject: 'Login SSO error setelah update', priority: 'High', sla: '2j 14m', agent: 'Wira S.', status: 'in-progress', csat: null },
  { id: 'TKT-1041', company: 'Hotel Aruna Bali', subject: 'Request training tambahan untuk 3 user', priority: 'Medium', sla: '1h 4j', agent: 'Tasya R.', status: 'open', csat: null },
  { id: 'TKT-1040', company: 'Kopi Senja Group', subject: 'Sinkronisasi menu antar outlet lambat', priority: 'High', sla: '36m', agent: 'Wira S.', status: 'escalated', csat: null },
  { id: 'TKT-1039', company: 'Klinik Prima Medika', subject: 'Export laporan PDF tidak bisa', priority: 'Medium', sla: '6j 22m', agent: 'Bayu K.', status: 'in-progress', csat: null },
  { id: 'TKT-1038', company: 'CV Maju Distribusi', subject: 'Onboarding user baru gudang Bekasi', priority: 'Low', sla: '2h 12j', agent: 'Tasya R.', status: 'waiting', csat: null },
  { id: 'TKT-1037', company: 'Sekolah Cendekia Mulia', subject: 'Permintaan custom report SPP', priority: 'Low', sla: '1h 8j', agent: 'Bayu K.', status: 'open', csat: null },
  { id: 'TKT-1036', company: 'Yayasan Pelita Bangsa', subject: 'Akses dashboard admin diperbarui', priority: 'Medium', sla: 'Resolved', agent: 'Wira S.', status: 'resolved', csat: 5 },
  { id: 'TKT-1035', company: 'Toko Laris Jaya', subject: 'Receipt printer tidak mau cetak', priority: 'High', sla: 'Resolved', agent: 'Tasya R.', status: 'resolved', csat: 4 },
];

const EMAILS = [
  { id: 1, from: 'Bayu Saputra', company: 'PT Sinar Retail', subject: 'Re: Proposal integrasi POS untuk Q3', preview: 'Halo Rina, kami sudah review. Tim IT punya pertanyaan…', when: '14:22', star: true, open: 4, click: 2 },
  { id: 2, from: 'Mira Anggraini', company: 'Kopi Senja Group', subject: 'Permintaan breakdown harga per outlet', preview: 'Hi Dimas, bisa share rincian biaya per outlet?', when: '13:05', star: false, open: 2, click: 1 },
  { id: 3, from: 'no-reply@calendar', company: 'System', subject: 'Reminder · Demo Klinik Prima Medika besok 10:00', preview: 'Demo session dengan dr. Anindya dijadwalkan besok…', when: '11:30', star: false, open: 1, click: 0 },
  { id: 4, from: 'Reza Mahendra', company: 'PT Bumi Hijau', subject: 'Kickoff onboarding tim implementasi', preview: 'Kami siap memulai onboarding. Tolong share timeline…', when: 'Kemarin', star: true, open: 6, click: 3 },
  { id: 5, from: 'I Made Wira', company: 'Hotel Aruna Bali', subject: 'Konfirmasi tanggal contract signing', preview: 'Untuk signing kontrak, kami pilih Jumat depan…', when: 'Kemarin', star: false, open: 3, click: 2 },
  { id: 6, from: 'Hendro K.', company: 'CV Maju Distribusi', subject: 'Klarifikasi modul multi-warehouse', preview: 'Dimas, terkait modul gudang, kami mau pastikan…', when: '2 hari lalu', star: false, open: 5, click: 1 },
];

const EMAIL_TEMPLATES = [
  { name: 'Follow-up Proposal', open: 64, click: 28, used: 142 },
  { name: 'Demo Invitation', open: 71, click: 41, used: 89 },
  { name: 'Onboarding Welcome', open: 92, click: 67, used: 56 },
  { name: 'Quarterly Check-in', open: 48, click: 22, used: 38 },
  { name: 'Renewal Reminder', open: 81, click: 55, used: 31 },
];

const SALES_REPS = [
  { name: 'Rina Wijaya', init: 'RW', deals: 18, won: 12, value: 2400000000, target: 3000000000, calls: 142, emails: 287 },
  { name: 'Dimas Pratama', init: 'DP', deals: 14, won: 8, value: 1450000000, target: 2000000000, calls: 118, emails: 241 },
  { name: 'Andini Sari', init: 'AS', deals: 11, won: 6, value: 820000000, target: 1500000000, calls: 96, emails: 198 },
  { name: 'Hendro Lukman', init: 'HL', deals: 9, won: 4, value: 540000000, target: 1200000000, calls: 78, emails: 162 },
];

const REVENUE_TREND = [
  { m: 'Jan', v: 480 }, { m: 'Feb', v: 620 }, { m: 'Mar', v: 540 }, { m: 'Apr', v: 720 },
  { m: 'Mei', v: 820 }, { m: 'Jun', v: 760 }, { m: 'Jul', v: 940 }, { m: 'Agu', v: 1080 },
  { m: 'Sep', v: 1240 }, { m: 'Okt', v: 1150 }, { m: 'Nov', v: 1380 }, { m: 'Des', v: 1620 },
];

const FUNNEL = [
  { stage: 'Visit', n: 12480, pct: 100 },
  { stage: 'Lead', n: 1840, pct: 14.7 },
  { stage: 'Qualified', n: 720, pct: 5.8 },
  { stage: 'Proposal', n: 312, pct: 2.5 },
  { stage: 'Won', n: 142, pct: 1.1 },
];

const DASHBOARD_STATS = {
  customers: 348,
  newLeads: 56,
  overdue: 9,
  pipeline: 5210000000,
  tickets: 14,
};

Object.assign(window, {
  fmtIDR, fmtShort, CUSTOMERS, ACTIVITIES, COMMUNICATIONS,
  PIPELINE_COLS, DEALS, REMINDERS, SEGMENTS, TICKETS, EMAILS,
  EMAIL_TEMPLATES, SALES_REPS, REVENUE_TREND, FUNNEL, DASHBOARD_STATS,
});
