/**
 * 为指定用户添加双币种可提现余额
 * 用法: node src/scripts/addUserBalanceDual.js <uid> <amountRmb> <amountPeso>
 * 示例: node src/scripts/addUserBalanceDual.js 704963 20 20
 */

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

async function addUserBalanceDual(uid, amountRmb, amountPeso) {
  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}`);
    console.log(`当前比索余额: ₱${user.balancePeso || 0}`);
    
    const oldBalanceRmb = parseFloat(user.balance) || 0;
    const oldBalancePeso = parseFloat(user.balancePeso) || 0;
    const newBalanceRmb = oldBalanceRmb + parseFloat(amountRmb);
    const newBalancePeso = oldBalancePeso + parseFloat(amountPeso);
    
    await user.update({ 
      balance: newBalanceRmb,
      balancePeso: newBalancePeso
    });
    
    console.log(`\n✅ 余额更新成功!`);
    console.log(`人民币余额:`);
    console.log(`   原余额: ¥${oldBalanceRmb}`);
    console.log(`   增加: +¥${amountRmb}`);
    console.log(`   新余额: ¥${newBalanceRmb}`);
    console.log(`比索余额:`);
    console.log(`   原余额: ₱${oldBalancePeso}`);
    console.log(`   增加: +₱${amountPeso}`);
    console.log(`   新余额: ₱${newBalancePeso}`);
    
  } catch (error) {
    console.error('操作失败:', error.message);
    process.exit(1);
  } finally {
    await sequelize.close();
  }
}

// 从命令行参数获取
const uid = process.argv[2];
const amountRmb = parseFloat(process.argv[3]) || 0;
const amountPeso = parseFloat(process.argv[4]) || 0;

if (!uid) {
  console.log('用法: node src/scripts/addUserBalanceDual.js <uid> <amountRmb> <amountPeso>');
  console.log('示例: node src/scripts/addUserBalanceDual.js 704963 20 20');
  process.exit(1);
}

addUserBalanceDual(uid, amountRmb, amountPeso);
