// catalog.jsx — Real product catalog from sticky.io spreadsheet.
// Layout: by product → by dose mg → by pills-per-month → by supply months.

const CHECKOUT_BASE = 'https://checkout.hermanrx.com/?id=';

// Sticky.io's URL param for coupons isn't publicly documented. Different
// implementations accept different keys, so we belt-and-suspenders by appending
// every common variant — sticky.io ignores unknowns harmlessly.
const COUPON_PARAM_KEYS = [
  'coupon',          // Checkout Page, SureCart, generic
  'coupon_code',     // some sticky.io configs
  'couponCode',      // camelCase variant
  'promo',           // Memberstack
  'promo_code',      // common
  'discount',        // Shopify
  'prefilled_promo_code', // Stripe
];

// Build "&coupon=X&promo=X&..." suffix to try every variant
function buildCouponSuffix(code) {
  if (!code) return '';
  return COUPON_PARAM_KEYS.map(k => `${k}=${encodeURIComponent(code)}`).join('&');
}

function url(id) { return CHECKOUT_BASE + id; }

// ─── Money formatting ──────────────────────────────────────────────────────
// Catalog totals may carry one decimal (e.g. $261.90). All subsequent math
// (discount subtraction, per-month division) can introduce float artifacts
// like $236.89999999999998 — round to cents on display.
function fmtMoney(n) {
  if (n == null || isNaN(n)) return '';
  // Snap to cents to kill float drift, then strip trailing .00 for cleaner integer prices
  const rounded = Math.round(Number(n) * 100) / 100;
  return Number.isInteger(rounded) ? `${rounded}` : rounded.toFixed(2);
}
// For whole-dollar contexts (per-month chips, save badges) round to integer
function fmtMoneyRound(n) {
  if (n == null || isNaN(n)) return '';
  return `${Math.round(Number(n))}`;
}

// ────────────────────────────────────────────────────────────────────────────
// Sildenafil 50mg and 100mg share the same on-demand pricing curve.
// Per-pill: 1mo=$10, 3mo=$8.50, 6mo=$8.00, 12mo=$6.00
// Pills/month picks: 6, 8, 10, 12, 14.
// ────────────────────────────────────────────────────────────────────────────
const SILD = {
  50: {
    6:  { 1: { id: 72, sku: 'SILD-50MG-6-6-1B',     total: 60 },
          3: { id: 65, sku: 'SILD-50MG-18-6-3B',    total: 153 },
          6: { id: 69, sku: 'SILD-50MG-36-6-6B',    total: 288 },
          12:{ id: 75, sku: 'SILD-50MG-72-6-12B',   total: 432 } },
    8:  { 1: { id: 76, sku: 'SILD-50MG-8-8-1B',     total: 80 },
          3: { id: 66, sku: 'SILD-50MG-24-8-3B',    total: 204 },
          6: { id: 71, sku: 'SILD-50MG-48-8-6B',    total: 384 },
          12:{ id: 78, sku: 'SILD-50MG-96-8-12B',   total: 576 } },
    10: { 1: { id: 59, sku: 'SILD-50MG-10-10-1B',   total: 100 },
          3: { id: 67, sku: 'SILD-50MG-30-10-3B',   total: 255 },
          6: { id: 73, sku: 'SILD-50MG-60-10-6B',   total: 480 },
          12:{ id: 61, sku: 'SILD-50MG-120-10-12B', total: 720 } },
    12: { 1: { id: 60, sku: 'SILD-50MG-12-12-1B',   total: 120 },
          3: { id: 68, sku: 'SILD-50MG-36-12-3B',   total: 306 },
          6: { id: 74, sku: 'SILD-50MG-72-12-6B',   total: 576 },
          12:{ id: 63, sku: 'SILD-50MG-144-12-12B', total: 864 } },
    14: { 1: { id: 62, sku: 'SILD-50MG-14-14-1B',   total: 140 },
          3: { id: 70, sku: 'SILD-50MG-42-14-3B',   total: 357 },
          6: { id: 77, sku: 'SILD-50MG-84-14-6B',   total: 672 },
          12:{ id: 64, sku: 'SILD-50MG-168-14-12B', total: 1008 } },
  },
  100: {
    6:  { 1: { id: 52, sku: 'SILD-100MG-6-6-1B',     total: 60 },
          3: { id: 45, sku: 'SILD-100MG-18-6-3B',    total: 153 },
          6: { id: 49, sku: 'SILD-100MG-36-6-6B',    total: 288 },
          12:{ id: 55, sku: 'SILD-100MG-72-6-12B',   total: 432 } },
    8:  { 1: { id: 56, sku: 'SILD-100MG-8-8-1B',     total: 80 },
          3: { id: 46, sku: 'SILD-100MG-24-8-3B',    total: 204 },
          6: { id: 51, sku: 'SILD-100MG-48-8-6B',    total: 384 },
          12:{ id: 58, sku: 'SILD-100MG-96-8-12B',   total: 576 } },
    10: { 1: { id: 39, sku: 'SILD-100MG-10-10-1B',   total: 100 },
          3: { id: 47, sku: 'SILD-100MG-30-10-3B',   total: 255 },
          6: { id: 53, sku: 'SILD-100MG-60-10-6B',   total: 480 },
          12:{ id: 41, sku: 'SILD-100MG-120-10-12B', total: 720 } },
    12: { 1: { id: 40, sku: 'SILD-100MG-12-12-1B',   total: 120 },
          3: { id: 48, sku: 'SILD-100MG-36-12-3B',   total: 306 },
          6: { id: 54, sku: 'SILD-100MG-72-12-6B',   total: 576 },
          12:{ id: 43, sku: 'SILD-100MG-144-12-12B', total: 864 } },
    14: { 1: { id: 42, sku: 'SILD-100MG-14-14-1B',   total: 140 },
          3: { id: 50, sku: 'SILD-100MG-42-14-3B',   total: 357 },
          6: { id: 57, sku: 'SILD-100MG-84-14-6B',   total: 672 },
          12:{ id: 44, sku: 'SILD-100MG-168-14-12B', total: 1008 } },
  },
};

// Tadalafil on-demand (10mg, 20mg) — same pricing curve as sildenafil
const TADA_OD = {
  10: {
    6:  { 1: { id: 92, sku: 'TADA-10MG-6-6-1B',     total: 60 },
          3: { id: 85, sku: 'TADA-10MG-18-6-3B',    total: 153 },
          6: { id: 89, sku: 'TADA-10MG-36-6-6B',    total: 288 },
          12:{ id: 95, sku: 'TADA-10MG-72-6-12B',   total: 432 } },
    8:  { 1: { id: 96, sku: 'TADA-10MG-8-8-1B',     total: 80 },
          3: { id: 86, sku: 'TADA-10MG-24-8-3B',    total: 204 },
          6: { id: 91, sku: 'TADA-10MG-48-8-6B',    total: 384 },
          12:{ id: 98, sku: 'TADA-10MG-96-8-12B',   total: 576 } },
    10: { 1: { id: 79, sku: 'TADA-10MG-10-10-1B',   total: 100 },
          3: { id: 87, sku: 'TADA-10MG-30-10-3B',   total: 255 },
          6: { id: 93, sku: 'TADA-10MG-60-10-6B',   total: 480 },
          12:{ id: 81, sku: 'TADA-10MG-120-10-12B', total: 720 } },
    12: { 1: { id: 80, sku: 'TADA-10MG-12-12-1B',   total: 120 },
          3: { id: 88, sku: 'TADA-10MG-36-12-3B',   total: 306 },
          6: { id: 94, sku: 'TADA-10MG-72-12-6B',   total: 576 },
          12:{ id: 83, sku: 'TADA-10MG-144-12-12B', total: 864 } },
    14: { 1: { id: 82, sku: 'TADA-10MG-14-14-1B',   total: 140 },
          3: { id: 90, sku: 'TADA-10MG-42-14-3B',   total: 357 },
          6: { id: 97, sku: 'TADA-10MG-84-14-6B',   total: 672 },
          12:{ id: 84, sku: 'TADA-10MG-168-14-12B', total: 1008 } },
  },
  20: {
    6:  { 1: { id: 116, sku: 'TADA-20MG-6-6-1B',     total: 60 },
          3: { id: 109, sku: 'TADA-20MG-18-6-3B',    total: 153 },
          6: { id: 113, sku: 'TADA-20MG-36-6-6B',    total: 288 },
          12:{ id: 119, sku: 'TADA-20MG-72-6-12B',   total: 432 } },
    8:  { 1: { id: 120, sku: 'TADA-20MG-8-8-1B',     total: 80 },
          3: { id: 110, sku: 'TADA-20MG-24-8-3B',    total: 204 },
          6: { id: 115, sku: 'TADA-20MG-48-8-6B',    total: 384 },
          12:{ id: 122, sku: 'TADA-20MG-96-8-12B',   total: 576 } },
    10: { 1: { id: 103, sku: 'TADA-20MG-10-10-1B',   total: 100 },
          3: { id: 111, sku: 'TADA-20MG-30-10-3B',   total: 255 },
          6: { id: 117, sku: 'TADA-20MG-60-10-6B',   total: 480 },
          12:{ id: 105, sku: 'TADA-20MG-120-10-12B', total: 720 } },
    12: { 1: { id: 104, sku: 'TADA-20MG-12-12-1B',   total: 120 },
          3: { id: 112, sku: 'TADA-20MG-36-12-3B',   total: 306 },
          6: { id: 118, sku: 'TADA-20MG-72-12-6B',   total: 576 },
          12:{ id: 107, sku: 'TADA-20MG-144-12-12B', total: 864 } },
    14: { 1: { id: 106, sku: 'TADA-20MG-14-14-1B',   total: 140 },
          3: { id: 114, sku: 'TADA-20MG-42-14-3B',   total: 357 },
          6: { id: 121, sku: 'TADA-20MG-84-14-6B',   total: 672 },
          12:{ id: 108, sku: 'TADA-20MG-168-14-12B', total: 1008 } },
  },
};

// Tadalafil daily (2.5mg, 5mg) — fixed 30/mo
const TADA_DAILY = {
  2.5: {
    30: { 1: { id: 100, sku: 'TADA-2.5MG-30-30-1B',   total: 90 },
          3: { id: 102, sku: 'TADA-2.5MG-90-30-3B',   total: 261.90 },
          6: { id: 99,  sku: 'TADA-2.5MG-180-30-6B',  total: 432 },
          12:{ id: 101, sku: 'TADA-2.5MG-360-30-12B', total: 648 } },
  },
  5: {
    30: { 1: { id: 124, sku: 'TADA-5MG-30-30-1B',   total: 90 },
          3: { id: 126, sku: 'TADA-5MG-90-30-3B',   total: 261.90 },
          6: { id: 123, sku: 'TADA-5MG-180-30-6B',  total: 432 },
          12:{ id: 125, sku: 'TADA-5MG-360-30-12B', total: 648 } },
  },
};

const CATALOG = {
  sildenafil: {
    id: 'sildenafil',
    name: 'Sildenafil',
    tagline: 'Generic Viagra — the on-demand classic',
    description: 'Take ~1 hour before; effects last 4\u20136 hours. Best for predictable, planned intimacy.',
    pill: 'blue',
    indication: 'On-demand ED',
    kind: 'on-demand',
    doses: [{ mg: 50, label: '50mg' }, { mg: 100, label: '100mg' }],
    qtys:  [6, 8, 10, 12, 14],
    durations: [1, 3, 6, 12],
    durationLabels: { 1: '1 month', 3: '3 months', 6: '6 months', 12: '12 months' },
    durationBadges: { 1: null, 3: 'Most popular', 6: null, 12: 'Best value' },
    skus: SILD,
    treats: ['ed'],
  },
  tadalafil: {
    id: 'tadalafil',
    name: 'Tadalafil',
    tagline: 'Generic Cialis — daily or on-demand',
    description: 'A low daily dose keeps you ready any moment. Or take 10\u201320mg on-demand for up to 36 hours of readiness.',
    pill: 'yellow',
    indication: 'Daily or on-demand ED',
    kind: 'mixed',
    // Two modes: daily (2.5/5mg) or on-demand (10/20mg)
    modes: [
      { id: 'daily',     label: 'Daily',     doses: [{mg:2.5, label:'2.5mg daily'}, {mg:5, label:'5mg daily'}],     qtys: [30], skus: TADA_DAILY },
      { id: 'on-demand', label: 'On-demand', doses: [{mg:10,  label:'10mg'},        {mg:20, label:'20mg'}],         qtys: [6, 8, 10, 12, 14], skus: TADA_OD },
    ],
    durations: [1, 3, 6, 12],
    durationLabels: { 1: '1 month', 3: '3 months', 6: '6 months', 12: '12 months' },
    durationBadges: { 1: null, 3: 'Most popular', 6: null, 12: 'Best value' },
    treats: ['ed'],
  },
  fits: {
    id: 'fits',
    name: 'FITS',
    tagline: '3-in-1 sexual performance formula',
    description: 'ED + PE + arousal \u2014 one daily tablet engineered for performance, calm, and confidence.',
    pill: 'fits',
    indication: 'ED + PE + Arousal',
    kind: 'coming-soon',
    treats: ['ed','pe','arousal'],
    durations: [1, 3, 6, 12],
    durationLabels: { 1: '1 month', 3: '3 months', 6: '6 months', 12: '12 months' },
    durationBadges: { 1: null, 3: 'Most popular', 6: null, 12: 'Best value' },
    // Placeholder pricing until FITS goes live
    placeholderPlans: [
      { duration: 1, pillsPerMonth: 30, total: 95 },
      { duration: 3, pillsPerMonth: 30, total: 237 },
      { duration: 6, pillsPerMonth: 30, total: 414 },
      { duration: 12, pillsPerMonth: 30, total: 708 },
    ],
  },
  sertraline: {
    id: 'sertraline',
    name: 'Sertraline',
    tagline: 'For lasting longer',
    description: 'A daily 25mg dose, clinically used for premature ejaculation.',
    pill: 'sertraline',
    indication: 'Premature ejaculation',
    kind: 'placeholder',
    treats: ['pe'],
    durations: [3, 6, 12],
    durationLabels: { 3: '3 months', 6: '6 months', 12: '12 months' },
    durationBadges: { 3: 'Starter', 6: 'Most popular', 12: 'Best value' },
    placeholderPlans: [
      { duration: 3, pillsPerMonth: 30, total: 126 },
      { duration: 6, pillsPerMonth: 30, total: 216 },
      { duration: 12, pillsPerMonth: 30, total: 348 },
    ],
  },
};

// ────────────────────────────────────────────────────────────────────────────
// Lookup helpers
// ────────────────────────────────────────────────────────────────────────────
function getSku(productId, mg, qty, duration, mode) {
  const p = CATALOG[productId];
  if (!p) return null;
  if (p.kind === 'on-demand') return p.skus?.[mg]?.[qty]?.[duration] || null;
  if (p.kind === 'mixed' && mode) {
    const m = p.modes.find(m => m.id === mode);
    return m?.skus?.[mg]?.[qty]?.[duration] || null;
  }
  return null;
}

function checkoutUrl(productId, mg, qty, duration, mode) {
  const sku = getSku(productId, mg, qty, duration, mode);
  if (!sku) return null;
  return url(sku.id);
}

// Find the "from" cheapest per-month price across all options for landing cards
function fromPricePerMonth(productId) {
  const p = CATALOG[productId];
  if (!p) return null;
  if (p.kind === 'placeholder') {
    const plans = p.placeholderPlans;
    return Math.min(...plans.map(pl => pl.total / pl.duration));
  }
  if (p.kind === 'coming-soon') {
    return Math.min(...p.placeholderPlans.map(pl => pl.total / pl.duration));
  }
  // Find cheapest per-month across all dose/qty/duration combos
  const collect = (skuTree) => {
    let min = Infinity;
    for (const mg of Object.keys(skuTree)) {
      for (const qty of Object.keys(skuTree[mg])) {
        for (const dur of Object.keys(skuTree[mg][qty])) {
          const total = skuTree[mg][qty][dur].total;
          const m = total / parseInt(dur);
          if (m < min) min = m;
        }
      }
    }
    return min;
  };
  if (p.kind === 'on-demand') return collect(p.skus);
  if (p.kind === 'mixed') {
    return Math.min(...p.modes.map(m => collect(m.skus)));
  }
  return null;
}

Object.assign(window, { CATALOG, getSku, checkoutUrl, fromPricePerMonth, CHECKOUT_BASE, fmtMoney, fmtMoneyRound, buildCouponSuffix });
