const { query } = require('../config/database')

async function run() {
  try {
    console.log('Inserting mock orders...')
    
    const orders = [
      {
        id: 'ORD202401010001',
        user_id: 1,
        status: 'completed',
        total_price: 156.80,
        cart: JSON.stringify([
          { goods_id: 1, name: '红富士苹果', price: 12.80, quantity: 5, unit: '斤' },
          { goods_id: 5, name: '土鸡蛋', price: 19.90, quantity: 2, unit: '盒' },
          { goods_id: 15, name: '可口可乐', price: 3.50, quantity: 6, unit: '瓶' }
        ]),
        user_info: JSON.stringify({ phone: '13800138000', name: '张三' })
      },
      {
        id: 'ORD202401020002',
        user_id: 1,
        status: 'paid',
        total_price: 89.70,
        cart: JSON.stringify([
          { goods_id: 3, name: '进口车厘子', price: 59.90, quantity: 1, unit: '斤' },
          { goods_id: 8, name: '蒙牛纯牛奶', price: 59.90, quantity: 1, unit: '箱' }
        ]),
        user_info: JSON.stringify({ phone: '13800138000', name: '张三' })
      },
      {
        id: 'ORD202401030003',
        user_id: 2,
        status: 'pending',
        total_price: 45.60,
        cart: JSON.stringify([
          { goods_id: 7, name: '金龙鱼花生油', price: 89.90, quantity: 1, unit: '桶' },
          { goods_id: 12, name: '卫龙辣条', price: 5.50, quantity: 3, unit: '包' }
        ]),
        user_info: JSON.stringify({ phone: '13900139000', name: '李四' })
      },
      {
        id: 'ORD202401040004',
        user_id: 2,
        status: 'completed',
        total_price: 123.40,
        cart: JSON.stringify([
          { goods_id: 10, name: '农夫山泉', price: 2.00, quantity: 12, unit: '瓶' },
          { goods_id: 14, name: '青岛啤酒', price: 5.00, quantity: 12, unit: '瓶' },
          { goods_id: 6, name: '五花肉', price: 28.80, quantity: 2, unit: '斤' }
        ]),
        user_info: JSON.stringify({ phone: '13900139000', name: '李四' })
      },
      {
        id: 'ORD202401050005',
        user_id: 1,
        status: 'cancelled',
        total_price: 299.90,
        cart: JSON.stringify([
          { goods_id: 3, name: '进口车厘子', price: 59.90, quantity: 5, unit: '斤' }
        ]),
        user_info: JSON.stringify({ phone: '13800138000', name: '张三' })
      }
    ]

    for (const order of orders) {
      await query(
        'INSERT INTO orders (id, user_id, status, total_price, cart, user_info) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE status=VALUES(status)',
        [order.id, order.user_id, order.status, order.total_price, order.cart, order.user_info]
      )
    }

    console.log('Inserting mock stock...')
    
    const stockItems = [
      { goods_id: 1, quantity: 100, warehouse: '默认仓库' },
      { goods_id: 2, quantity: 200, warehouse: '默认仓库' },
      { goods_id: 3, quantity: 50, warehouse: '默认仓库' },
      { goods_id: 4, quantity: 80, warehouse: '默认仓库' },
      { goods_id: 5, quantity: 120, warehouse: '默认仓库' },
      { goods_id: 6, quantity: 60, warehouse: '默认仓库' },
      { goods_id: 7, quantity: 40, warehouse: '默认仓库' },
      { goods_id: 8, quantity: 30, warehouse: '默认仓库' },
      { goods_id: 9, quantity: 150, warehouse: '默认仓库' },
      { goods_id: 10, quantity: 300, warehouse: '默认仓库' },
      { goods_id: 11, quantity: 200, warehouse: '默认仓库' },
      { goods_id: 12, quantity: 500, warehouse: '默认仓库' },
      { goods_id: 13, quantity: 200, warehouse: '默认仓库' },
      { goods_id: 14, quantity: 150, warehouse: '默认仓库' },
      { goods_id: 15, quantity: 500, warehouse: '默认仓库' }
    ]

    for (const stock of stockItems) {
      await query(
        'INSERT INTO stock (goods_id, quantity, warehouse) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE quantity=VALUES(quantity)',
        [stock.goods_id, stock.quantity, stock.warehouse]
      )
    }

    console.log('Inserting mock points logs...')
    
    const pointsLogs = [
      { user_id: 1, type: 'earn', amount: 1000, description: '新用户注册赠送' },
      { user_id: 1, type: 'spend', amount: 200, description: '积分兑换商品' },
      { user_id: 1, type: 'earn', amount: 50, description: '购物满100元奖励' },
      { user_id: 2, type: 'earn', amount: 500, description: '新用户注册赠送' },
      { user_id: 2, type: 'earn', amount: 30, description: '购物满50元奖励' }
    ]

    for (const log of pointsLogs) {
      await query(
        'INSERT INTO points_logs (user_id, type, amount, description) VALUES (?, ?, ?, ?)',
        [log.user_id, log.type, log.amount, log.description]
      )
    }

    console.log('✅ Mock data inserted successfully!')
    
    console.log('\n--- Data Summary ---')
    const categories = await query('SELECT COUNT(*) as count FROM categories')
    const goods = await query('SELECT COUNT(*) as count FROM goods')
    const users = await query('SELECT COUNT(*) as count FROM users')
    const ordersCount = await query('SELECT COUNT(*) as count FROM orders')
    const stockCount = await query('SELECT COUNT(*) as count FROM stock')
    const logsCount = await query('SELECT COUNT(*) as count FROM points_logs')
    
    console.log(`分类: ${categories[0]?.count || 0} 条`)
    console.log(`商品: ${goods[0]?.count || 0} 条`)
    console.log(`用户: ${users[0]?.count || 0} 条`)
    console.log(`订单: ${ordersCount[0]?.count || 0} 条`)
    console.log(`库存: ${stockCount[0]?.count || 0} 条`)
    console.log(`积分记录: ${logsCount[0]?.count || 0} 条`)
    
    process.exit(0)
  } catch (error) {
    console.error('Failed to insert mock data:', error)
    process.exit(1)
  }
}

run()