/**
 * Flow Momentum Score Calculator
 * Calculates a 0-100 score indicating how strong the flow momentum is
 * Higher score = stronger institutional conviction
 */

/**
 * Calculate flow momentum score
 * @param {Object} row - Flow row with badges, scores, and context
 * @param {Object} trendData - Flow trend data from flowTrendDetector
 * @returns {number} Momentum score (0-100)
 */
export function calculateFlowMomentum(row, trendData) {
  let score = 0;
  
  // Base: Rocket score (0-30 points)
  // Rocket score is 0-10, scale to 0-30
  const rocketScore = row.rocketScore || row.rocket_score || 0;
  score += rocketScore * 3;
  
  // Trend contribution (0-25 points)
  if (trendData?.trend === 'SURGING') {
    score += 25;
  } else if (trendData?.trend === 'STABLE') {
    score += 10;
  } else if (trendData?.trend === 'FADING') {
    score -= 15;
  } else if (trendData?.trend === 'DEAD') {
    score -= 25;
  }
  
  // Volume spike (0-15 points)
  if (trendData?.volumeSpike) {
    score += 15;
  } else if (trendData?.volumeRatio && trendData.volumeRatio > 1.5) {
    score += 8; // Moderate volume increase
  }
  
  // Tape alignment (0-10 points)
  if (row.tapeAligned || row.tape_alignment === 1) {
    score += 10;
  }
  
  // Catalyst (0-10 points)
  if (row.near_alert_type || row.catalyst_flag) {
    score += 10;
  }
  
  // Recency (0-10 points) - fresher flow = better
  const minutesAgo = trendData?.lastFlowMinutesAgo ?? 999;
  if (minutesAgo < 5) {
    score += 10; // Very fresh
  } else if (minutesAgo < 15) {
    score += 7; // Fresh
  } else if (minutesAgo < 30) {
    score += 4; // Recent
  } else if (minutesAgo >= 60) {
    score -= 10; // Stale
  }
  
  // Premium size bonus (0-5 points)
  const premium = row.premium_num || 0;
  if (premium > 2000000) {
    score += 5; // Huge premium
  } else if (premium > 1000000) {
    score += 3; // Large premium
  } else if (premium > 500000) {
    score += 1; // Medium premium
  }
  
  // Cap at 0-100
  return Math.max(0, Math.min(100, Math.round(score)));
}

/**
 * Get momentum label and color
 * @param {number} score - Momentum score
 * @returns {Object} Label and color info
 */
export function getMomentumLabel(score) {
  if (score >= 80) {
    return { label: 'EXTREME', color: 'green', icon: '🔥🔥' };
  } else if (score >= 70) {
    return { label: 'VERY HIGH', color: 'green', icon: '🔥' };
  } else if (score >= 60) {
    return { label: 'HIGH', color: 'green', icon: '🟢' };
  } else if (score >= 50) {
    return { label: 'MODERATE', color: 'yellow', icon: '🟡' };
  } else if (score >= 40) {
    return { label: 'LOW', color: 'orange', icon: '🟠' };
  } else {
    return { label: 'VERY LOW', color: 'red', icon: '🔴' };
  }
}

