'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Add approval_status column
    await queryInterface.addColumn('business_partner', 'approval_status', {
      type: Sequelize.ENUM('PENDING', 'APPROVED', 'REJECTED'),
      allowNull: false,
      defaultValue: 'PENDING',
      comment: 'Approval status for business partner'
    });

    // Add approved_by column
    await queryInterface.addColumn('business_partner', 'approved_by', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: {
        model: 'user',
        key: 'id'
      },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL',
      comment: 'User who approved the business partner'
    });

    // Add approved_at column
    await queryInterface.addColumn('business_partner', 'approved_at', {
      type: Sequelize.DATE,
      allowNull: true,
      comment: 'Date and time when business partner was approved'
    });

    // Add rejected_by column
    await queryInterface.addColumn('business_partner', 'rejected_by', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: {
        model: 'user',
        key: 'id'
      },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL',
      comment: 'User who rejected the business partner'
    });

    // Add rejected_at column
    await queryInterface.addColumn('business_partner', 'rejected_at', {
      type: Sequelize.DATE,
      allowNull: true,
      comment: 'Date and time when business partner was rejected'
    });

    // Add rejection_reason column
    await queryInterface.addColumn('business_partner', 'rejection_reason', {
      type: Sequelize.TEXT,
      allowNull: true,
      comment: 'Reason for rejection'
    });

    // Add index on approval_status for faster queries
    await queryInterface.addIndex('business_partner', ['approval_status'], {
      name: 'business_partner_approval_status_idx'
    });
  },

  async down(queryInterface, Sequelize) {
    // Remove index
    await queryInterface.removeIndex('business_partner', 'business_partner_approval_status_idx');

    // Remove columns
    await queryInterface.removeColumn('business_partner', 'rejection_reason');
    await queryInterface.removeColumn('business_partner', 'rejected_at');
    await queryInterface.removeColumn('business_partner', 'rejected_by');
    await queryInterface.removeColumn('business_partner', 'approved_at');
    await queryInterface.removeColumn('business_partner', 'approved_by');
    await queryInterface.removeColumn('business_partner', 'approval_status');
  }
};
