// 데이터 + 계산 로직. 라벨은 i18n 키(t('pattern.male.mild_label') 등)로 lookup.
// 이 파일은 ID와 수치(계산용)만 보관.

const PATTERNS = [
  { id: 'mild',   grafts: [800, 1200],  img: 'assets/male_mild.png' },
  { id: 'm',      grafts: [1500, 2200], img: 'assets/male_m.png' },
  { id: 'crown',  grafts: [1800, 2800], img: 'assets/male_crown.png' },
  { id: 'mc',     grafts: [2800, 4000], img: 'assets/male_mc.png' },
  { id: 'wide',   grafts: [4000, 5500], img: 'assets/male_wide.png' },
  { id: 'severe', grafts: [5500, 7500], img: 'assets/male_severe.png' },
];

const FEMALE_PATTERNS = [
  { id: 'round',    grafts: [800, 1500],  img: 'assets/female/round.png' },
  { id: 'fm',       grafts: [1000, 1800], img: 'assets/female/m.png' },
  { id: 'rect',     grafts: [1200, 2000], img: 'assets/female/rectangle.png' },
  { id: 'bell',     grafts: [1000, 1800], img: 'assets/female/bell.png' },
  { id: 'triangle', grafts: [800, 1500],  img: 'assets/female/triangle.png' },
];

const AREAS = [
  { id: 'hairline', mult: 0.95, icon: 'hairline' },
  { id: 'mzone',    mult: 0.95, icon: 'mzone' },
  { id: 'top',      mult: 1.0,  icon: 'top' },
  { id: 'whole',    mult: 1.25, icon: 'whole' },
];

const DENSITY = [
  { id: 'natural',  mult: 0.9 },
  { id: 'standard', mult: 1.0 },
  { id: 'full',     mult: 1.2 },
];

// Calculation — gender·age 보정과 헤어라인 내림 가산 포함
function calculateGrafts({ patternId, areaIds, densityId, hairlineLower, age, gender }) {
  const isFemale = gender === 'female';
  const patternList = isFemale ? FEMALE_PATTERNS : PATTERNS;
  const pattern = patternList.find(p => p.id === patternId);
  if (!pattern) return null;
  let [low, high] = pattern.grafts;

  let areaMult = 1.0;
  if (areaIds && areaIds.length) {
    const mults = areaIds.map(id => (AREAS.find(a => a.id === id)?.mult ?? 1));
    areaMult = Math.max(...mults);
    if (areaIds.length >= 3) areaMult *= 1.05;
  }
  low = Math.round(low * areaMult);
  high = Math.round(high * areaMult);

  const dens = DENSITY.find(d => d.id === densityId);
  if (dens) {
    low = Math.round(low * dens.mult);
    high = Math.round(high * dens.mult);
  }

  if (age && age < 30) { low = Math.round(low * 1.05); high = Math.round(high * 1.1); }
  if (age && age >= 50) { low = Math.round(low * 0.92); high = Math.round(high * 0.95); }

  if (hairlineLower && /^[1-3]$/.test(hairlineLower)) {
    const cm = parseInt(hairlineLower, 10);
    const add = cm * 1000;
    low += add;
    high += add;
  }

  low = Math.round(low / 100) * 100;
  high = Math.round(high / 100) * 100;
  const mid = Math.round(((low + high) / 2) / 100) * 100;

  return { low, high, mid };
}

// SVG glyphs
const Glyph = ({ name, size = 22, color = 'currentColor' }) => {
  const props = { width: size, height: size, viewBox: '0 0 24 24', fill: 'none', stroke: color, strokeWidth: 1.8, strokeLinecap: 'round', strokeLinejoin: 'round' };
  switch (name) {
    case 'hairline':
      return <svg {...props}><path d="M4 11 Q 12 4 20 11" /><path d="M5 11 V 18 H 19 V 11" /></svg>;
    case 'mzone':
      return <svg {...props}><path d="M4 11 Q 7 5 10 9 Q 12 7 14 9 Q 17 5 20 11" /><path d="M5 11 V 18 H 19 V 11" /></svg>;
    case 'top':
      return <svg {...props}><circle cx="12" cy="12" r="8" /><circle cx="12" cy="12" r="3" fill={color} stroke="none"/></svg>;
    case 'whole':
      return <svg {...props}><circle cx="12" cy="12" r="8" /><path d="M8 9 H 16 M8 12 H 16 M8 15 H 16" /></svg>;
    case 'check':
      return <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"><polyline points="20 6 9 17 4 12"/></svg>;
    case 'phone':
      return <svg {...props} strokeWidth={2}><path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.13.96.37 1.9.72 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.91.35 1.85.59 2.81.72A2 2 0 0 1 22 16.92z"/></svg>;
    case 'kakao':
      return <svg width={size} height={size} viewBox="0 0 24 24" fill={color}><path d="M12 3C6.48 3 2 6.48 2 10.8c0 2.7 1.78 5.07 4.46 6.43-.16.6-.6 2.27-.69 2.62-.1.43.16.43.34.31.14-.09 2.23-1.51 3.13-2.13.92.13 1.86.2 2.76.2 5.52 0 10-3.48 10-7.43S17.52 3 12 3z"/></svg>;
    case 'camera':
      return <svg {...props}><path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z"/><circle cx="12" cy="13" r="4"/></svg>;
    case 'shield':
      return <svg {...props}><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>;
    case 'clock':
      return <svg {...props}><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>;
    case 'sparkle':
      return <svg {...props}><path d="M12 3v3M12 18v3M3 12h3M18 12h3M5.6 5.6l2.1 2.1M16.3 16.3l2.1 2.1M5.6 18.4l2.1-2.1M16.3 7.7l2.1-2.1"/></svg>;
    case 'info':
      return <svg {...props}><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>;
    case 'arrow':
      return <svg {...props} strokeWidth={2}><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>;
    case 'globe':
      return <svg {...props}><circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/></svg>;
    default: return null;
  }
};

window.PATTERNS = PATTERNS;
window.FEMALE_PATTERNS = FEMALE_PATTERNS;
window.AREAS = AREAS;
window.DENSITY = DENSITY;
window.calculateGrafts = calculateGrafts;
window.Glyph = Glyph;

window.AREA_IMG = {"hairline":"assets/high.png","mzone":"assets/m.png","top":"assets/crown.png","whole":"assets/all.png"};
window.AREA_IMG_FEMALE = {"hairline":"assets/female/high.png","mzone":"assets/female/m.png","top":"assets/female/crown.png","whole":"assets/female/all.png"};

// ────────── 수가(견적) 데이터 ──────────
// 단위: 만원. 모발이식 단가는 1,000모 기준(부가세 포함).
const METHODS = [
  { id: 'incision',  vat: 198 },
  { id: 'minshave',  vat: 209 },
  { id: 'lineshave', vat: 231 },
  { id: 'partshave', vat: 253 },
  { id: 'noshave',   vat: 275 },
];

// alone: 단독 가격, bundle: 모발이식 동시 시술 시 패키지 추가금 (만원)
const STEMCELL = [
  { id: 'none', rounds: 0,  alone: 0,   bundle: 0 },
  { id: 's5',   rounds: 5,  alone: 280, bundle: 150 },
  { id: 's12',  rounds: 12, alone: 330, bundle: 200 },
  { id: 's20',  rounds: 20, alone: 380, bundle: 250 },
];

function calcPrice(grafts, methodId, stemId) {
  const m = METHODS.find(x => x.id === methodId);
  if (!m) return { transplant: 0, stem: 0, total: 0, stemSavings: 0, stemAlone: 0, perGraft: 0 };
  const transplant = (m.vat * grafts) / 1000;
  const s = STEMCELL.find(x => x.id === stemId) || STEMCELL[0];
  const stem = s.bundle;
  const stemSavings = s.alone > 0 ? s.alone - s.bundle : 0;
  return {
    transplant,
    stem,
    total: transplant + stem,
    stemSavings,
    stemAlone: s.alone,
    perGraft: m.vat * 10,
  };
}

window.METHODS = METHODS;
window.STEMCELL = STEMCELL;
window.calcPrice = calcPrice;
// 호환용: 이전 시그니처를 i18n-aware 포맷터로 위임
window.wonFmt = function (manwon) { return window.wonFmtI18n ? window.wonFmtI18n(manwon) : Math.round(manwon * 10000).toLocaleString('ko-KR') + '원'; };
window.manwonFmt = function (manwon) { return window.manwonFmtI18n ? window.manwonFmtI18n(manwon) : (manwon === 0 ? '0원' : manwon.toLocaleString('ko-KR') + '만원'); };
