export function calculateBadges(row) {
  const badges = {
    round: '',
    more: '',
    flash: '',
    rocket: ''
  };

  // 🟢/🔴 logic
  if (row.bull_prem_itm > row.bear_prem_itm) {
    badges.round = '🟢';
  } else if (row.bull_prem_itm < row.bear_prem_itm) {
    badges.round = '🔴';
  }

  // 💎 logic (ITM dominance)
  if (badges.round === '🟢' && row.prem_cb_itm > row.prem_ps_itm) {
    badges.more += '💎';
  }
  if (badges.round === '🔴' && row.prem_pb_itm > row.prem_cs_itm) {
    badges.more += '💎';
  }

  // ⭐ logic (OTM flow)
  const otm_bull_raw = row.prem_cb_otm + row.prem_ps_otm;
  const otm_bear_raw = row.prem_pb_otm + row.prem_cs_otm;
  if (badges.round === '🟢' && (otm_bull_raw - otm_bear_raw) > 10000) {
    badges.more += '⭐';
  }
  if (badges.round === '🔴' && (otm_bear_raw - otm_bull_raw) > 10000) {
    badges.more += '⭐';
  }

  // 💰 logic (OI > 100K)
  if (badges.round === '🟢' && row.oi_cb_otm > 100000) {
    badges.more += '💰';
  }
  if (badges.round === '🔴' && row.oi_pb_otm > 100000) {
    badges.more += '💰';
  }

  // ✔ logic (Vol > OI = new positioning)
  if (row.vol_num > row.oi_num) {
    badges.more += '✔';
  }

  // ⚡ flash (aggressive side sweep + premium > 10K)
  const isAggressiveBull = 
    (row.cp_norm === 'CALL' && row.side_norm?.includes('AA')) ||
    (row.cp_norm === 'PUT' && row.side_norm?.includes('BB'));
  const isAggressiveBear = 
    (row.cp_norm === 'CALL' && row.side_norm?.includes('BB')) ||
    (row.cp_norm === 'PUT' && row.side_norm?.includes('AA'));

  if (row.premium_num > 10000) {
    if ((badges.round === '🟢' && isAggressiveBull) ||
        (badges.round === '🔴' && isAggressiveBear)) {
      badges.flash = '⚡';
    }
  }

  // 🚀 logic (compound rocket)
  badges.rocket = calculateRocket(row, badges);

  return badges;
}

function calculateRocket(row, badges) {
  // All stars aligned
  if (badges.round === '🟢' && 
      badges.more.includes('💎') && 
      badges.more.includes('⭐') && 
      badges.more.includes('💰') && 
      badges.flash === '⚡' && 
      row.premium_num > 1000000) {
    return '🚀';
  }

  // Vol > OI + flash + money + premium > 500K
  if (row.vol_num > row.oi_num && 
      badges.flash === '⚡' && 
      badges.more.includes('💰') && 
      row.premium_num > 500000) {
    return '🚀🚀🚀';
  }

  // Vol > OI + flash + money
  if (row.vol_num > row.oi_num && 
      badges.flash === '⚡' && 
      badges.more.includes('💰')) {
    return '🚀🚀';
  }

  // Vol > OI + (flash OR premium > 500K)
  if (row.vol_num > row.oi_num && 
      (badges.flash === '⚡' || row.premium_num > 500000)) {
    return '🚀';
  }

  return '';
}
