/**
 * Moneyness Helper
 * Determines how far in/out of the money an option is
 * Critical for identifying gamma squeeze setups and strike selection
 */

/**
 * Get moneyness context for an option
 * @param {Object} row - Flow row with strike, spot, and call/put info
 * @returns {Object} Moneyness details
 */
export function getMoneynessContext(row) {
  const strike = row.strike_num || parseFloat(row.Strike) || 0;
  const spot = row.spot_num || parseFloat(row.Spot) || 0;
  const cp = row.cp_norm || row.CallPut?.toUpperCase() || '';
  
  if (!strike || !spot || strike === 0 || spot === 0) {
    return {
      category: 'UNKNOWN',
      distance: 0,
      distancePct: 0,
      isITM: false,
      isOTM: false,
      isATM: false
    };
  }
  
  let distance;
  let distancePct;
  let isITM = false;
  let isOTM = false;
  let isATM = false;
  let category;
  
  if (cp === 'CALL' || cp.includes('CALL')) {
    distance = strike - spot;
    distancePct = (distance / spot) * 100;
    
    if (distance < -0.02 * spot) {
      category = 'DEEP_ITM';
      isITM = true;
    } else if (distance < 0) {
      category = 'ITM';
      isITM = true;
    } else if (Math.abs(distance) < 0.01 * spot) {
      category = 'NEAR_ATM';
      isATM = true;
    } else if (distance < 0.05 * spot) {
      category = 'OTM';
      isOTM = true;
    } else if (distance < 0.10 * spot) {
      category = 'DEEP_OTM';
      isOTM = true;
    } else {
      category = 'EXTREME_OTM';
      isOTM = true;
    }
  } else if (cp === 'PUT' || cp.includes('PUT')) {
    distance = spot - strike;
    distancePct = (distance / spot) * 100;
    
    if (distance < -0.02 * spot) {
      category = 'DEEP_ITM';
      isITM = true;
    } else if (distance < 0) {
      category = 'ITM';
      isITM = true;
    } else if (Math.abs(distance) < 0.01 * spot) {
      category = 'NEAR_ATM';
      isATM = true;
    } else if (distance < 0.05 * spot) {
      category = 'OTM';
      isOTM = true;
    } else if (distance < 0.10 * spot) {
      category = 'DEEP_OTM';
      isOTM = true;
    } else {
      category = 'EXTREME_OTM';
      isOTM = true;
    }
  } else {
    return {
      category: 'UNKNOWN',
      distance: 0,
      distancePct: 0,
      isITM: false,
      isOTM: false,
      isATM: false
    };
  }
  
  return {
    category,
    distance,
    distancePct: Math.round(distancePct * 10) / 10, // Round to 1 decimal
    isITM,
    isOTM,
    isATM,
    strike,
    spot
  };
}

/**
 * Check if this is a gamma squeeze setup
 * @param {Object} row - Flow row
 * @returns {boolean} True if gamma squeeze setup
 */
export function isGammaSqueezeSetup(row) {
  const moneyness = getMoneynessContext(row);
  const badges = row.badges || row.badgesRaw || {};
  const hasFullBadges = badges.round === '🟢' && 
                       badges.more?.includes('💎') &&
                       badges.more?.includes('⭐') &&
                       badges.more?.includes('💰') &&
                       badges.flash === '⚡';
  
  // Gamma squeeze: Deep OTM calls with full institutional badges
  return (moneyness.category === 'DEEP_OTM' || moneyness.category === 'EXTREME_OTM') &&
         (row.cp_norm === 'CALL' || row.CallPut?.toUpperCase().includes('CALL')) &&
         hasFullBadges;
}

/**
 * Get moneyness display label
 * @param {Object} moneyness - Moneyness context object
 * @returns {string} Display label
 */
export function getMoneynessLabel(moneyness) {
  const labels = {
    'DEEP_ITM': 'Deep ITM',
    'ITM': 'ITM',
    'NEAR_ATM': 'Near ATM',
    'OTM': 'OTM',
    'DEEP_OTM': 'Deep OTM',
    'EXTREME_OTM': 'Extreme OTM',
    'UNKNOWN': 'Unknown'
  };
  
  const label = labels[moneyness.category] || 'Unknown';
  const pct = moneyness.distancePct !== 0 
    ? ` (${moneyness.distancePct > 0 ? '+' : ''}${moneyness.distancePct}%)`
    : '';
  
  return `${label}${pct}`;
}

