/**
 * Migration: Add cost_peso and cost_rmb fields to payment_orders table
 */
const { sequelize } = require('./src/config/database');

async function migrate() {
  try {
    const queryInterface = sequelize.getQueryInterface();

    // Check if columns already exist
    const tableDesc = await queryInterface.describeTable('payment_orders');

    if (!tableDesc.cost_peso) {
      await queryInterface.addColumn('payment_orders', 'cost_peso', {
        type: require('sequelize').DataTypes.DECIMAL(10, 2),
        allowNull: true,
        defaultValue: null,
        comment: '成本金额（比索）',
      });
      console.log('Added cost_peso column');
    } else {
      console.log('cost_peso column already exists');
    }

    if (!tableDesc.cost_rmb) {
      await queryInterface.addColumn('payment_orders', 'cost_rmb', {
        type: require('sequelize').DataTypes.DECIMAL(10, 2),
        allowNull: true,
        defaultValue: null,
        comment: '成本金额（人民币）',
      });
      console.log('Added cost_rmb column');
    } else {
      console.log('cost_rmb column already exists');
    }

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

migrate();
