/**
 * Migration: Add cancel_reason field to payment_orders table
 */

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

async function up() {
  const queryInterface = sequelize.getQueryInterface();
  
  // Check if column already exists
  const tableInfo = await queryInterface.describeTable('payment_orders');
  
  if (!tableInfo.cancel_reason) {
    await queryInterface.addColumn('payment_orders', 'cancel_reason', {
      type: 'TEXT',
      allowNull: true,
      comment: '取消原因'
    });
    console.log('Added cancel_reason column');
  }
  
  console.log('Migration completed successfully');
}

async function down() {
  const queryInterface = sequelize.getQueryInterface();
  await queryInterface.removeColumn('payment_orders', 'cancel_reason');
  console.log('Rollback completed');
}

// Run migration
if (require.main === module) {
  up()
    .then(() => process.exit(0))
    .catch(err => {
      console.error('Migration failed:', err);
      process.exit(1);
    });
}

module.exports = { up, down };
