/**
 * Script to add dual currency balance fields
 * - Add balance_peso to user table
 * - Add currency to commissions table
 * - Add currency to withdrawal table
 */

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

const addDualCurrencyFields = async () => {
  try {
    console.log('Starting dual currency fields migration...');

    // Add balance_peso to user table
    try {
      await sequelize.query(`
        ALTER TABLE user 
        ADD COLUMN balance_peso DECIMAL(10, 2) NOT NULL DEFAULT 0.00 
        COMMENT '比索余额'
      `);
      console.log('✓ Added balance_peso to user table');
    } catch (error) {
      if (error.message.includes('Duplicate column')) {
        console.log('⊘ balance_peso already exists in user table');
      } else {
        throw error;
      }
    }

    // Add currency to commissions table
    try {
      await sequelize.query(`
        ALTER TABLE commissions 
        ADD COLUMN currency ENUM('RMB', 'PHP') NOT NULL DEFAULT 'RMB' 
        COMMENT '佣金货币类型'
      `);
      console.log('✓ Added currency to commissions table');
    } catch (error) {
      if (error.message.includes('Duplicate column')) {
        console.log('⊘ currency already exists in commissions table');
      } else {
        throw error;
      }
    }

    // Add currency to withdrawal table
    try {
      await sequelize.query(`
        ALTER TABLE withdrawal 
        ADD COLUMN currency ENUM('CNY', 'PHP') NOT NULL DEFAULT 'CNY' 
        COMMENT '提现货币类型'
      `);
      console.log('✓ Added currency to withdrawal table');
    } catch (error) {
      if (error.message.includes('Duplicate column')) {
        console.log('⊘ currency already exists in withdrawal table');
      } else {
        throw error;
      }
    }

    console.log('\n========================================');
    console.log('Dual currency migration completed!');
    console.log('========================================\n');

    process.exit(0);
  } catch (error) {
    console.error('Migration failed:', error);
    process.exit(1);
  }
};

addDualCurrencyFields();
