import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  console.log('🌱 开始种子数据...');

  // 创建期货品种
  const products = [
    { symbol: 'SC', name: '原油', category: 'energy', exchange: 'INE', unit: '元/桶', minChange: 0.1 },
    { symbol: 'AU', name: '黄金', category: 'metal', exchange: 'SHFE', unit: '元/克', minChange: 0.02 },
    { symbol: 'CU', name: '铜', category: 'metal', exchange: 'SHFE', unit: '元/吨', minChange: 10 },
    { symbol: 'AG', name: '白银', category: 'metal', exchange: 'SHFE', unit: '元/千克', minChange: 1 },
    { symbol: 'I', name: '铁矿石', category: 'metal', exchange: 'DCE', unit: '元/吨', minChange: 0.5 },
    { symbol: 'M', name: '豆粕', category: 'agriculture', exchange: 'DCE', unit: '元/吨', minChange: 1 },
    { symbol: 'P', name: '棕榈油', category: 'agriculture', exchange: 'DCE', unit: '元/吨', minChange: 2 },
    { symbol: 'EC', name: '集运指数', category: 'financial', exchange: 'INE', unit: '点', minChange: 0.1 },
    { symbol: 'RB', name: '螺纹钢', category: 'metal', exchange: 'SHFE', unit: '元/吨', minChange: 1 },
    { symbol: 'FG', name: '玻璃', category: 'industrial', exchange: 'ZCE', unit: '元/吨', minChange: 1 },
    { symbol: 'SA', name: '纯碱', category: 'industrial', exchange: 'ZCE', unit: '元/吨', minChange: 1 },
    { symbol: 'MA', name: '甲醇', category: 'energy', exchange: 'ZCE', unit: '元/吨', minChange: 1 },
    { symbol: 'TA', name: 'PTA', category: 'industrial', exchange: 'ZCE', unit: '元/吨', minChange: 2 },
    { symbol: 'CF', name: '棉花', category: 'agriculture', exchange: 'ZCE', unit: '元/吨', minChange: 5 },
    { symbol: 'SR', name: '白糖', category: 'agriculture', exchange: 'ZCE', unit: '元/吨', minChange: 1 },
  ];

  for (const product of products) {
    await prisma.product.upsert({
      where: { symbol: product.symbol },
      update: product,
      create: product,
    });
  }

  console.log(`✅ 已创建 ${products.length} 个期货品种`);

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

  for (const event of events) {
    const existing = await prisma.hotEvent.findFirst({
      where: { title: event.title },
    });

    if (!existing) {
      // 查找产品ID
      const products = await prisma.product.findMany({
        where: { symbol: { in: event.affectedSymbols } },
      });

      await prisma.hotEvent.create({
        data: {
          title: event.title,
          summary: event.summary,
          content: event.content,
          impact: event.impact,
          impactLevel: event.impactLevel,
          analysis: event.analysis,
          risks: event.risks,
          affectedProducts: {
            create: products.map((p) => ({
              productId: p.id,
              impactConfidence: 0.8,
            })),
          },
        },
      });
    }
  }

  console.log(`✅ 已创建 ${events.length} 个热点事件`);

  // 创建系统配置
  const configs = [
    { key: 'MARKET_STATUS', value: 'open', description: '市场状态' },
    { key: 'TRADING_HOURS', value: '09:00-11:30,13:30-15:00,21:00-02:30', description: '交易时间' },
  ];

  for (const config of configs) {
    await prisma.systemConfig.upsert({
      where: { key: config.key },
      update: config,
      create: config,
    });
  }

  console.log(`✅ 已创建 ${configs.length} 个系统配置`);

  console.log('✨ 种子数据完成！');
}

main()
  .catch((e) => {
    console.error('❌ 种子数据失败:', e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
