/* ===== Covera sample data ===== */
const THB = n => '฿' + Number(n).toLocaleString('en-US');
const THBshort = n => {
  n = Number(n);
  if (n >= 1e6) return '฿' + (n/1e6).toLocaleString('en-US',{maximumFractionDigits:1}) + ' ล้าน';
  if (n >= 1e3) return '฿' + Math.round(n/1e3) + 'K';
  return '฿' + n.toLocaleString('en-US');
};
const TH_MONTHS = ['ม.ค.','ก.พ.','มี.ค.','เม.ย.','พ.ค.','มิ.ย.','ก.ค.','ส.ค.','ก.ย.','ต.ค.','พ.ย.','ธ.ค.'];
const TH_MONTHS_FULL = ['มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน','กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม'];
// date stored as {d, m(1-12), y(BE)}
const fmtBE = dt => dt ? `${dt.d} ${TH_MONTHS[dt.m-1]} ${dt.y}` : '';
const ageFrom = dt => 2568 - dt.y; // current BE year 2568
const maskPolicy = no => '••• ' + no.slice(-3);

// coverage categories meta
const CATS = [
  { key:'life',     name:'ทุนชีวิต',      icon:'shieldCheck', color:'#1e40af' },
  { key:'ci',       name:'โรคร้ายแรง',    icon:'pulse',       color:'#db2777' },
  { key:'health',   name:'สุขภาพ',        icon:'heart',       color:'#0891b2' },
  { key:'accident', name:'อุบัติเหตุ',     icon:'crash',       color:'#ea580c' },
  { key:'income',   name:'ชดเชยรายได้',   icon:'income',      color:'#7c3aed' },
  { key:'property', name:'ทรัพย์สิน',      icon:'home',        color:'#0d9488' },
];

const COMPANIES = [
  { id:'co1', name:'ไทยประกันชีวิต',        color:'#1e3a8a', init:'ทป' },
  { id:'co2', name:'เมืองไทยประกันชีวิต',   color:'#db2777', init:'มท' },
  { id:'co3', name:'เอไอเอ ประเทศไทย',      color:'#c2410c', init:'AIA' },
  { id:'co4', name:'กรุงเทพประกันชีวิต',    color:'#38bdf8', init:'BLA' },
  { id:'co5', name:'พรูเด็นเชียล',          color:'#6d28d9', init:'PRU' },
  { id:'co6', name:'อลิอันซ์ อยุธยา',       color:'#0e7490', init:'AZ' },
];

const PLAN_TYPES = ['ตลอดชีพ','สะสมทรัพย์','บำนาญ','ยูนิตลิงค์','ชั่วระยะเวลา'];
const FREQ = ['รายปี','ราย 6 เดือน','รายไตรมาส','รายเดือน'];

// helper to build cash-value timeline (years of premium accumulation vs surrender value)
function cvTimeline(premium, years, sumInsured){
  const arr=[];
  for(let y=1;y<=years;y++){
    const paid = premium*y;
    // surrender value: grows, crosses paid premium around year 8-10
    const cv = Math.round(sumInsured * (0.03*y + 0.0009*y*y) );
    arr.push({ y, paid, cv });
  }
  return arr;
}

const AV_COLORS = ['#1e40af','#0891b2','#7c3aed','#db2777','#ea580c','#0d9488','#b45309','#4f46e5'];
const initials = name => name.trim().charAt(0);

let CUSTOMERS = [
  {
    id:'c1', name:'สมชาย วัฒนกุล', gender:'ชาย', birth:{d:14,m:3,y:2528}, job:'วิศวกรโยธา',
    phone:'081-234-5678', email:'somchai.w@email.com', avatar:AV_COLORS[0], tag:'ครบกำหนด',
    coverage:{ life:2000000, ci:1000000, health:0, accident:500000, income:0, property:0 },
    income:90000,
    policies:[
      { id:'p1', co:'co1', plan:'ตลอดชีพ', no:'8842310482', sum:2000000, premium:48000, freq:'รายปี', start:2562, years:8,
        riders:[{n:'โรคร้ายแรง',s:1000000},{n:'อุบัติเหตุ (PA)',s:500000}],
        bene:[{n:'สมหญิง วัฒนกุล',rel:'คู่สมรส',pct:60},{n:'ด.ช. ปกรณ์ วัฒนกุล',rel:'บุตร',pct:40}] },
      { id:'p2', co:'co3', plan:'สะสมทรัพย์', no:'1190034827', sum:500000, premium:52000, freq:'รายปี', start:2565, years:5,
        riders:[], bene:[{n:'สมหญิง วัฒนกุล',rel:'คู่สมรส',pct:100}] },
    ],
  },
  {
    id:'c2', name:'ปิยะดา ศรีสุข', gender:'หญิง', birth:{d:2,m:11,y:2533}, job:'เจ้าของกิจการ',
    phone:'089-887-1200', email:'piyada.s@email.com', avatar:AV_COLORS[3], tag:'ค้างติดตาม',
    coverage:{ life:1000000, ci:0, health:300000, accident:0, income:0, property:0 },
    income:120000,
    policies:[
      { id:'p3', co:'co2', plan:'ยูนิตลิงค์', no:'7730019945', sum:1000000, premium:120000, freq:'รายปี', start:2566, years:3,
        riders:[{n:'สุขภาพเหมาจ่าย',s:300000}], bene:[{n:'วิชัย ศรีสุข',rel:'บิดา',pct:100}] },
    ],
  },
  {
    id:'c3', name:'อนุชา เรืองโรจน์', gender:'ชาย', birth:{d:25,m:7,y:2521}, job:'แพทย์',
    phone:'086-555-3344', email:'anucha.r@email.com', avatar:AV_COLORS[2], tag:'',
    coverage:{ life:5000000, ci:2000000, health:1000000, accident:1000000, income:50000, property:0 },
    income:250000,
    policies:[
      { id:'p4', co:'co4', plan:'ตลอดชีพ', no:'5521008873', sum:5000000, premium:185000, freq:'รายปี', start:2558, years:11,
        riders:[{n:'โรคร้ายแรง',s:2000000},{n:'สุขภาพเหมาจ่าย',s:1000000},{n:'ชดเชยรายได้',s:50000}],
        bene:[{n:'รัตนา เรืองโรจน์',rel:'คู่สมรส',pct:50},{n:'น.ส. มนัสนันท์',rel:'บุตร',pct:50}] },
      { id:'p5', co:'co6', plan:'บำนาญ', no:'9980142200', sum:1500000, premium:100000, freq:'รายปี', start:2564, years:5,
        riders:[], bene:[{n:'รัตนา เรืองโรจน์',rel:'คู่สมรส',pct:100}] },
    ],
  },
  {
    id:'c4', name:'กมลวรรณ พงษ์ไพบูลย์', gender:'หญิง', birth:{d:19,m:1,y:2538}, job:'นักออกแบบ',
    phone:'062-401-9988', email:'kamol.p@email.com', avatar:AV_COLORS[5], tag:'',
    coverage:{ life:500000, ci:0, health:0, accident:200000, income:0, property:0 },
    income:55000,
    policies:[
      { id:'p6', co:'co5', plan:'สะสมทรัพย์', no:'3310077612', sum:500000, premium:30000, freq:'ราย 6 เดือน', start:2567, years:2,
        riders:[{n:'อุบัติเหตุ (PA)',s:200000}], bene:[{n:'สุนทร พงษ์ไพบูลย์',rel:'บิดา',pct:100}] },
    ],
  },
  {
    id:'c5', name:'ธนกร อินทรพานิช', gender:'ชาย', birth:{d:8,m:9,y:2516}, job:'ผู้บริหาร',
    phone:'081-999-0011', email:'thanakorn.i@email.com', avatar:AV_COLORS[6], tag:'ครบกำหนด',
    coverage:{ life:10000000, ci:3000000, health:2000000, accident:2000000, income:100000, property:5000000 },
    income:400000,
    policies:[
      { id:'p7', co:'co1', plan:'ตลอดชีพ', no:'8810229934', sum:10000000, premium:420000, freq:'รายปี', start:2553, years:15,
        riders:[{n:'โรคร้ายแรง',s:3000000},{n:'สุขภาพเหมาจ่าย',s:2000000},{n:'อุบัติเหตุ (PA)',s:2000000}],
        bene:[{n:'พิมพ์ใจ อินทรพานิช',rel:'คู่สมรส',pct:50},{n:'ธนภัทร อินทรพานิช',rel:'บุตร',pct:25},{n:'ธนพร อินทรพานิช',rel:'บุตร',pct:25}] },
    ],
  },
  {
    id:'c6', name:'วราภรณ์ จันทร์เพ็ญ', gender:'หญิง', birth:{d:30,m:5,y:2542}, job:'พยาบาล',
    phone:'090-223-7745', email:'waraporn.j@email.com', avatar:AV_COLORS[4], tag:'ค้างติดตาม',
    coverage:{ life:300000, ci:500000, health:500000, accident:0, income:0, property:0 },
    income:42000,
    policies:[
      { id:'p8', co:'co2', plan:'ชั่วระยะเวลา', no:'7740583310', sum:300000, premium:8000, freq:'รายปี', start:2566, years:3,
        riders:[{n:'โรคร้ายแรง',s:500000},{n:'สุขภาพ',s:500000}], bene:[{n:'ประยูร จันทร์เพ็ญ',rel:'มารดา',pct:100}] },
    ],
  },
];

// recommended coverage by category (target for gap analysis), based on income*multiple
function targets(c){
  const annual = c.income*12;
  return {
    life: Math.max(annual*5, 1000000),
    ci: 1000000,
    health: 1000000,
    accident: annual*3,
    income: c.income,
    property: 2000000,
  };
}
function coveragePct(c){
  const t = targets(c); let have=0, need=0;
  CATS.forEach(cat=>{ const tt=t[cat.key]; const hv=Math.min(c.coverage[cat.key]||0, tt); have+=hv; need+=tt; });
  return Math.round(have/need*100);
}
function gaps(c){
  return CATS.filter(cat => (c.coverage[cat.key]||0) === 0);
}

// financial pyramid (per customer) — 4 layers each 0..100
function pyramidFor(c){
  const pct = coveragePct(c);
  const g = gaps(c).length;
  const base = Math.max(20, pct);                          // foundation = protection coverage
  const save = c.id==='c5'?78:c.id==='c3'?70:c.id==='c1'?45:c.id==='c2'?35:c.id==='c4'?40:30;
  const invest = c.id==='c5'?72:c.id==='c3'?60:c.id==='c2'?50:25;
  const legacy = c.id==='c5'?65:c.id==='c3'?40:20;
  const score = Math.round(base*0.4 + save*0.25 + invest*0.2 + legacy*0.15);
  return { base, save, invest, legacy, score, gaps:g };
}

Object.assign(window, {
  THB, THBshort, TH_MONTHS, TH_MONTHS_FULL, fmtBE, ageFrom, maskPolicy,
  CATS, COMPANIES, PLAN_TYPES, FREQ, cvTimeline, initials, AV_COLORS,
  CUSTOMERS, targets, coveragePct, gaps, pyramidFor,
});
