/**
 * Stock Universe Service
 * Manages the top 50 stocks + ETFs universe with cap tier classification
 */

import NodeCache from 'node-cache';

const cache = new NodeCache({ stdTTL: 60 }); // 60 second cache

// Cap tier thresholds
const CAP_TIERS = {
  LARGE: 10_000_000_000,  // > $10B
  MID: 2_000_000_000,     // $2B - $10B
  // Below $2B = SMALL
};

// Master universe of top 50 stocks + ETFs
export const UNIVERSE = [
  // Mega-cap Tech
  { symbol: 'AAPL', name: 'Apple Inc.', sector: 'Technology', isETF: false },
  { symbol: 'MSFT', name: 'Microsoft Corp.', sector: 'Technology', isETF: false },
  { symbol: 'NVDA', name: 'NVIDIA Corp.', sector: 'Technology', isETF: false },
  { symbol: 'AMZN', name: 'Amazon.com Inc.', sector: 'Consumer Discret.', isETF: false },
  { symbol: 'GOOGL', name: 'Alphabet Inc.', sector: 'Technology', isETF: false },
  { symbol: 'META', name: 'Meta Platforms', sector: 'Technology', isETF: false },
  { symbol: 'TSLA', name: 'Tesla Inc.', sector: 'Consumer Discret.', isETF: false },
  { symbol: 'AVGO', name: 'Broadcom Inc.', sector: 'Technology', isETF: false },
  { symbol: 'ORCL', name: 'Oracle Corp.', sector: 'Technology', isETF: false },
  { symbol: 'AMD', name: 'Advanced Micro Devices', sector: 'Technology', isETF: false },
  // Financials
  { symbol: 'JPM', name: 'JPMorgan Chase', sector: 'Financials', isETF: false },
  { symbol: 'BAC', name: 'Bank of America', sector: 'Financials', isETF: false },
  { symbol: 'WFC', name: 'Wells Fargo', sector: 'Financials', isETF: false },
  { symbol: 'GS', name: 'Goldman Sachs', sector: 'Financials', isETF: false },
  { symbol: 'V', name: 'Visa Inc.', sector: 'Financials', isETF: false },
  { symbol: 'MA', name: 'Mastercard Inc.', sector: 'Financials', isETF: false },
  // Healthcare
  { symbol: 'UNH', name: 'UnitedHealth Group', sector: 'Healthcare', isETF: false },
  { symbol: 'LLY', name: 'Eli Lilly & Co.', sector: 'Healthcare', isETF: false },
  { symbol: 'JNJ', name: 'Johnson & Johnson', sector: 'Healthcare', isETF: false },
  { symbol: 'ABBV', name: 'AbbVie Inc.', sector: 'Healthcare', isETF: false },
  { symbol: 'PFE', name: 'Pfizer Inc.', sector: 'Healthcare', isETF: false },
  // Energy
  { symbol: 'XOM', name: 'Exxon Mobil', sector: 'Energy', isETF: false },
  { symbol: 'CVX', name: 'Chevron Corp.', sector: 'Energy', isETF: false },
  // Industrials
  { symbol: 'CAT', name: 'Caterpillar Inc.', sector: 'Industrials', isETF: false },
  { symbol: 'HON', name: 'Honeywell Intl.', sector: 'Industrials', isETF: false },
  // Consumer
  { symbol: 'WMT', name: 'Walmart Inc.', sector: 'Consumer Staples', isETF: false },
  { symbol: 'COST', name: 'Costco Wholesale', sector: 'Consumer Staples', isETF: false },
  // Semiconductors
  { symbol: 'QCOM', name: 'Qualcomm Inc.', sector: 'Technology', isETF: false },
  { symbol: 'MU', name: 'Micron Technology', sector: 'Technology', isETF: false },
  { symbol: 'INTC', name: 'Intel Corp.', sector: 'Technology', isETF: false },
  // Growth / Momentum
  { symbol: 'PLTR', name: 'Palantir Technologies', sector: 'Technology', isETF: false },
  { symbol: 'CRWD', name: 'CrowdStrike Holdings', sector: 'Technology', isETF: false },
  { symbol: 'SNOW', name: 'Snowflake Inc.', sector: 'Technology', isETF: false },
  { symbol: 'COIN', name: 'Coinbase Global', sector: 'Financials', isETF: false },
  { symbol: 'NFLX', name: 'Netflix Inc.', sector: 'Technology', isETF: false },
  { symbol: 'CRM', name: 'Salesforce Inc.', sector: 'Technology', isETF: false },
  { symbol: 'ADBE', name: 'Adobe Inc.', sector: 'Technology', isETF: false },
  { symbol: 'NOW', name: 'ServiceNow Inc.', sector: 'Technology', isETF: false },
  { symbol: 'MSTR', name: 'MicroStrategy Inc.', sector: 'Technology', isETF: false },
  // Broad ETFs
  { symbol: 'SPY', name: 'SPDR S&P 500 ETF', sector: 'ETF', isETF: true },
  { symbol: 'QQQ', name: 'Invesco QQQ Trust', sector: 'ETF', isETF: true },
  { symbol: 'IWM', name: 'iShares Russell 2000', sector: 'ETF', isETF: true },
  { symbol: 'DIA', name: 'SPDR Dow Jones ETF', sector: 'ETF', isETF: true },
  // Sector ETFs
  { symbol: 'XLF', name: 'Financial Select SPDR', sector: 'ETF', isETF: true },
  { symbol: 'XLE', name: 'Energy Select SPDR', sector: 'ETF', isETF: true },
  { symbol: 'XLK', name: 'Technology Select SPDR', sector: 'ETF', isETF: true },
  { symbol: 'XLV', name: 'Health Care Select SPDR', sector: 'ETF', isETF: true },
  // Specialty ETFs
  { symbol: 'ARKK', name: 'ARK Innovation ETF', sector: 'ETF', isETF: true },
  { symbol: 'GLD', name: 'SPDR Gold Shares', sector: 'ETF', isETF: true },
  { symbol: 'TLT', name: 'iShares 20+ Yr Treasury', sector: 'ETF', isETF: true },
];

/**
 * Classify market cap tier
 */
export function classifyCapTier(marketCap, isETF = false) {
  if (isETF) return 'ETF';
  if (!marketCap || marketCap === 0) return 'UNKNOWN';
  if (marketCap >= CAP_TIERS.LARGE) return 'LARGE';
  if (marketCap >= CAP_TIERS.MID) return 'MID';
  return 'SMALL';
}

/**
 * Get universe symbols list
 */
export function getUniverseSymbols() {
  return UNIVERSE.map(s => s.symbol);
}

/**
 * Get universe meta by symbol
 */
export function getSymbolMeta(symbol) {
  return UNIVERSE.find(s => s.symbol === symbol.toUpperCase()) || null;
}

/**
 * Get all universe entries
 */
export function getUniverse() {
  return UNIVERSE;
}
