/**
 * 为指定用户添加可提现余额
 * 用法: node src/scripts/addUserBalance.js <uid> <amount>
 * 示例: node src/scripts/addUserBalance.js 782593 200
 */

require('dotenv').config();
const { sequelize } = require('../config/database');
const User = require('../models/User');

async function addUserBalance(uid, amount) {
  try {
    console.log(`正在查找用户 UID: ${uid}...`);
    
    const user = await User.findOne({ where: { uid } });
    
    if (!user) {
      console.error(`错误: 未找到 UID 为 ${uid} 的用户`);
      process.exit(1);
    }
    
    console.log(`找到用户: ${user.nickname || user.realName || '未设置昵称'}`);
    console.log(`当前余额: ${user.balance}`);
    
    const newBalance = parseFloat(user.balance) + parseFloat(amount);
    
    await user.update({ balance: newBalance });
    
    console.log(`✅ 余额更新成功!`);
    console.log(`   原余额: ${user.balance - amount}`);
    console.log(`   增加: +${amount}`);
    console.log(`   新余额: ${newBalance}`);
    
  } catch (error) {
    console.error('操作失败:', error.message);
    process.exit(1);
  } finally {
    await sequelize.close();
  }
}

// 从命令行参数获取
const uid = process.argv[2] || '782593';
const amount = parseFloat(process.argv[3]) || 200;

if (!uid) {
  console.log('用法: node src/scripts/addUserBalance.js <uid> <amount>');
  console.log('示例: node src/scripts/addUserBalance.js 782593 200');
  process.exit(1);
}

addUserBalance(uid, amount);
