import type { FuturesProduct, HotEvent, MarketOverview, RiskAlert, CycleAnalysis, KLineData, MACDData, TechnicalIndicators, TradingAdvice } from '@/types';

// 市场概览数据
export const marketOverview: MarketOverview = {
  heatIndex: 78.5,
  heatChange: 5.2,
  upCount: 156,
  downCount: 89,
  capitalFlow: 28.5,
  volatilityIndex: 23.8,
  volatilityChange: -1.2,
};

// 热点事件数据
export const hotEvents: HotEvent[] = [
  {
    id: '1',
    title: '地缘政治风险升级',
    time: '2025-03-02',
    summary: '美以袭伊朗，"海上油阀"被关，中东局势紧张升级',
    affectedProducts: ['原油', '黄金', '白银', '燃油'],
    impact: 'bullish',
    impactLevel: 5,
    analysis: '地缘政治风险急剧升温，霍尔木兹海峡封锁风险上升。原油供应中断担忧推动油价上涨，避险资产黄金、白银同步走强。短期油价易涨难跌，建议关注原油、黄金相关品种做多机会。',
    risks: ['冲突升级可能', '供应中断风险', '波动率激增'],
  },
  {
    id: '2',
    title: '黄金价格创历史新高',
    time: '2025-03-01',
    summary: 'COMEX黄金突破3100美元关口，避险需求强劲',
    affectedProducts: ['黄金', '白银', '铂金', '美元指数'],
    impact: 'bullish',
    impactLevel: 4,
    analysis: '特朗普关税政策"2.0"版本引发市场担忧，叠加地缘政治风险，黄金避险属性凸显。技术面突破历史高点，多头趋势强劲。预计金价短期维持强势，回调即是买入机会。',
    risks: ['获利回吐压力', '美元反弹风险', '美联储政策转向'],
  },
  {
    id: '3',
    title: '铜供应紧张预期',
    time: '2025-02-28',
    summary: '全球铜市场预计出现18万吨供应缺口，美国或加征铜进口关税',
    affectedProducts: ['铜', '铝', '镍', '锌'],
    impact: 'bullish',
    impactLevel: 4,
    analysis: '铜精矿TC现货指数持续回落，矿山供应增长放缓。美国可能加征铜进口关税，刺激美铜价格上涨，全球套利行为收紧供应。需求端新能源产业用铜需求快速增长，供需矛盾支撑铜价。',
    risks: ['需求放缓风险', '美元走强压制', '库存累积'],
  },
  {
    id: '4',
    title: '美联储通胀数据超预期',
    time: '2025-02-27',
    summary: '核心PCE数据超预期，降息预期降温',
    affectedProducts: ['美元指数', '黄金', '原油', '大宗商品'],
    impact: 'bearish',
    impactLevel: 3,
    analysis: '美国2月核心PCE通胀数据超预期，市场对美联储降息预期降温。美元指数短期获得支撑，对大宗商品形成一定压制。但地缘政治风险对冲了部分利空影响。',
    risks: ['加息预期升温', '美元持续走强', '风险资产抛售'],
  },
  {
    id: '5',
    title: 'OPEC+减产延期预期',
    time: '2025-02-26',
    summary: 'OPEC+可能延长减产协议至二季度末',
    affectedProducts: ['原油', '燃油', '沥青', '石化品种'],
    impact: 'bullish',
    impactLevel: 3,
    analysis: 'OPEC+成员国倾向于延长减产协议以支撑油价。全球原油需求预期改善，叠加供应端约束，原油市场供需格局趋紧。关注减产协议正式落地情况。',
    risks: ['减产执行力度', '非OPEC增产', '需求不及预期'],
  },
];

// 期货品种数据
export const futuresProducts: FuturesProduct[] = [
  {
    id: '1',
    name: '原油',
    code: 'SC',
    category: 'energy',
    price: 528.6,
    change: 12.1,
    changePercent: 2.35,
    open: 518.0,
    high: 535.0,
    low: 515.5,
    volume: 285600,
    openInterest: 125800,
    trendScore: 85,
    successRate: 72,
    cycles: {
      m5: 'up',
      m15: 'up',
      m30: 'up',
      m60: 'sideways',
    },
    keyLevels: {
      resistance: [535.0, 542.0, 550.0],
      support: [518.0, 510.0, 500.0],
    },
    recommendation: 'long',
    recommendationReason: '地缘政治风险推动，多周期共振向上',
  },
  {
    id: '2',
    name: '黄金',
    code: 'AU',
    category: 'metal',
    price: 685.2,
    change: 12.45,
    changePercent: 1.85,
    open: 675.0,
    high: 692.0,
    low: 672.0,
    volume: 456200,
    openInterest: 215600,
    trendScore: 92,
    successRate: 78,
    cycles: {
      m5: 'up',
      m15: 'up',
      m30: 'up',
      m60: 'up',
    },
    keyLevels: {
      resistance: [692.0, 700.0, 710.0],
      support: [678.0, 670.0, 660.0],
    },
    recommendation: 'long',
    recommendationReason: '创历史新高，趋势强劲，避险需求旺盛',
  },
  {
    id: '3',
    name: '铜',
    code: 'CU',
    category: 'metal',
    price: 80610,
    change: 112,
    changePercent: 0.14,
    open: 80200,
    high: 81200,
    low: 79800,
    volume: 125800,
    openInterest: 98500,
    trendScore: 65,
    successRate: 58,
    cycles: {
      m5: 'sideways',
      m15: 'up',
      m30: 'sideways',
      m60: 'up',
    },
    keyLevels: {
      resistance: [81200, 82000, 83000],
      support: [79800, 79000, 78000],
    },
    recommendation: 'wait',
    recommendationReason: '高位震荡，方向不明，建议观望',
  },
  {
    id: '4',
    name: '白银',
    code: 'AG',
    category: 'metal',
    price: 8250,
    change: 165,
    changePercent: 2.04,
    open: 8100,
    high: 8350,
    low: 8050,
    volume: 325600,
    openInterest: 185200,
    trendScore: 88,
    successRate: 75,
    cycles: {
      m5: 'up',
      m15: 'up',
      m30: 'up',
      m60: 'up',
    },
    keyLevels: {
      resistance: [8350, 8500, 8650],
      support: [8100, 8000, 7850],
    },
    recommendation: 'long',
    recommendationReason: '跟随黄金上涨，波动更大，机会更好',
  },
  {
    id: '5',
    name: '铁矿石',
    code: 'I',
    category: 'metal',
    price: 785.5,
    change: 28.0,
    changePercent: 3.7,
    open: 760.0,
    high: 792.0,
    low: 755.0,
    volume: 185200,
    openInterest: 95600,
    trendScore: 82,
    successRate: 68,
    cycles: {
      m5: 'up',
      m15: 'up',
      m30: 'up',
      m60: 'up',
    },
    keyLevels: {
      resistance: [792.0, 800.0, 815.0],
      support: [770.0, 760.0, 745.0],
    },
    recommendation: 'long',
    recommendationReason: '黑色系领涨，需求预期改善',
  },
  {
    id: '6',
    name: '豆粕',
    code: 'M',
    category: 'agriculture',
    price: 2985,
    change: -51,
    changePercent: -1.68,
    open: 3040,
    high: 3050,
    low: 2960,
    volume: 256800,
    openInterest: 325600,
    trendScore: 35,
    successRate: 42,
    cycles: {
      m5: 'down',
      m15: 'down',
      m30: 'down',
      m60: 'sideways',
    },
    keyLevels: {
      resistance: [3050, 3100, 3180],
      support: [2960, 2900, 2820],
    },
    recommendation: 'short',
    recommendationReason: '供应充足，需求疲软，短期偏弱',
  },
  {
    id: '7',
    name: '棕榈油',
    code: 'P',
    category: 'agriculture',
    price: 8750,
    change: 0,
    changePercent: 0,
    open: 8720,
    high: 8820,
    low: 8680,
    volume: 125600,
    openInterest: 185200,
    trendScore: 50,
    successRate: 52,
    cycles: {
      m5: 'sideways',
      m15: 'sideways',
      m30: 'sideways',
      m60: 'sideways',
    },
    keyLevels: {
      resistance: [8820, 8900, 9050],
      support: [8680, 8600, 8450],
    },
    recommendation: 'wait',
    recommendationReason: '横盘整理，等待方向选择',
  },
  {
    id: '8',
    name: '集运指数',
    code: 'EC',
    category: 'financial',
    price: 2150,
    change: 196,
    changePercent: 10.06,
    open: 1960,
    high: 2200,
    low: 1940,
    volume: 85600,
    openInterest: 45600,
    trendScore: 90,
    successRate: 80,
    cycles: {
      m5: 'up',
      m15: 'up',
      m30: 'up',
      m60: 'up',
    },
    keyLevels: {
      resistance: [2200, 2300, 2400],
      support: [2000, 1900, 1800],
    },
    recommendation: 'long',
    recommendationReason: '涨停突破，地缘风险推升运价',
  },
];

// 风险提醒数据
export const riskAlerts: RiskAlert[] = [
  {
    id: '1',
    title: '中东地缘风险急剧升温',
    affectedProducts: ['原油', '黄金', '燃油'],
    riskLevel: 'high',
    description: '美以与伊朗冲突升级，霍尔木兹海峡封锁风险上升',
    suggestion: '控制仓位，设置止损，关注局势发展',
    time: '2025-03-02 14:30',
  },
  {
    id: '2',
    title: '黄金价格创历史新高',
    affectedProducts: ['黄金', '白银'],
    riskLevel: 'medium',
    description: '金价快速上涨后存在获利回吐风险',
    suggestion: '避免追高，等待回调后再入场',
    time: '2025-03-01 09:15',
  },
  {
    id: '3',
    title: '集运指数涨停',
    affectedProducts: ['集运指数'],
    riskLevel: 'high',
    description: '连续涨停后波动率激增，注意回调风险',
    suggestion: '减仓或获利了结，避免隔夜重仓',
    time: '2025-03-02 15:00',
  },
];

// 生成K线数据
export function generateKLineData(basePrice: number, count: number = 100): KLineData[] {
  const data: KLineData[] = [];
  let price = basePrice;
  const now = new Date();
  
  for (let i = count; i >= 0; i--) {
    const time = new Date(now.getTime() - i * 5 * 60 * 1000);
    const volatility = price * 0.008;
    const change = (Math.random() - 0.48) * volatility;
    
    const open = price;
    const close = price + change;
    const high = Math.max(open, close) + Math.random() * volatility * 0.5;
    const low = Math.min(open, close) - Math.random() * volatility * 0.5;
    const volume = Math.floor(Math.random() * 10000 + 5000);
    
    data.push({
      time: time.toISOString(),
      open: Number(open.toFixed(2)),
      high: Number(high.toFixed(2)),
      low: Number(low.toFixed(2)),
      close: Number(close.toFixed(2)),
      volume,
    });
    
    price = close;
  }
  
  return data;
}

// 生成MACD数据
export function generateMACDData(klineData: KLineData[]): MACDData[] {
  const data: MACDData[] = [];
  let ema12 = klineData[0].close;
  let ema26 = klineData[0].close;
  let dea = 0;
  
  const k12 = 2 / 13;
  const k26 = 2 / 27;
  const k9 = 2 / 10;
  
  for (const kline of klineData) {
    ema12 = kline.close * k12 + ema12 * (1 - k12);
    ema26 = kline.close * k26 + ema26 * (1 - k26);
    const dif = ema12 - ema26;
    dea = dif * k9 + dea * (1 - k9);
    const macd = (dif - dea) * 2;
    
    data.push({
      time: kline.time,
      dif: Number(dif.toFixed(4)),
      dea: Number(dea.toFixed(4)),
      macd: Number(macd.toFixed(4)),
    });
  }
  
  return data;
}

// 生成周期分析数据
export function generateCycleAnalysis(productId: string, period: 'm5' | 'm15' | 'm30' | 'm60'): CycleAnalysis {
  const product = futuresProducts.find(p => p.id === productId);
  const basePrice = product?.price || 500;
  const klineData = generateKLineData(basePrice, 80);
  const macdData = generateMACDData(klineData);
  
  const volumeData = klineData.map(d => ({
    time: d.time,
    value: d.volume,
  }));
  
  return {
    period,
    trend: product?.cycles[period] || 'sideways',
    trendStrength: Math.floor(Math.random() * 40) + 60,
    klineData,
    volumeData,
    macdData,
    keyLevels: {
      resistance: product?.keyLevels.resistance || [],
      support: product?.keyLevels.support || [],
    },
  };
}

// 生成技术指标
export function generateTechnicalIndicators(productId: string): TechnicalIndicators {
  const product = futuresProducts.find(p => p.id === productId);
  const price = product?.price || 500;
  
  return {
    macd: {
      signal: Math.random() > 0.5 ? 'golden_cross' : 'neutral',
      value: Number((Math.random() * 2 - 0.5).toFixed(4)),
    },
    rsi: {
      value: Math.floor(Math.random() * 40) + 30,
      status: 'normal',
    },
    bollinger: {
      upper: price * 1.03,
      middle: price,
      lower: price * 0.97,
      position: 'middle',
    },
    kdj: {
      k: Math.floor(Math.random() * 100),
      d: Math.floor(Math.random() * 100),
      j: Math.floor(Math.random() * 100),
      signal: Math.random() > 0.6 ? 'golden_cross' : 'neutral',
    },
  };
}

// 生成交易建议
export function generateTradingAdvice(productId: string): TradingAdvice {
  const product = futuresProducts.find(p => p.id === productId);
  const price = product?.price || 500;
  const recommendation = product?.recommendation || 'wait';
  
  const volatility = price * 0.02;
  
  return {
    action: recommendation,
    entryPrice: recommendation === 'long' ? price - volatility * 0.3 : price + volatility * 0.3,
    stopLoss: recommendation === 'long' ? price - volatility : price + volatility,
    targetPrice: recommendation === 'long' ? price + volatility * 2 : price - volatility * 2,
    riskLevel: product?.trendScore && product.trendScore > 80 ? 'low' : product?.trendScore && product.trendScore > 60 ? 'medium' : 'high',
    position: product?.trendScore && product.trendScore > 80 ? 'medium' : 'light',
    holdingTime: 'short',
    reason: product?.recommendationReason || '技术面与基本面综合判断',
  };
}
