'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 1. Change 'source' column type from ENUM to STRING
    await queryInterface.changeColumn('SystemProfits', 'source', {
      type: Sequelize.STRING(50),
      allowNull: false
    });

    // 2. Add new columns
    await queryInterface.addColumn('SystemProfits', 'customerId', {
      type: Sequelize.UUID,
      allowNull: true,
      references: {
        model: 'Customers',
        key: 'id'
      },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL'
    });

    await queryInterface.addColumn('SystemProfits', 'feeProfit', {
      type: Sequelize.DECIMAL(20, 2),
      defaultValue: 0.00
    });

    await queryInterface.addColumn('SystemProfits', 'marginProfit', {
      type: Sequelize.DECIMAL(20, 2),
      defaultValue: 0.00
    });

    await queryInterface.addColumn('SystemProfits', 'currency', {
      type: Sequelize.STRING(10),
      defaultValue: 'NGN'
    });

    await queryInterface.addColumn('SystemProfits', 'providerFee', {
      type: Sequelize.DECIMAL(20, 2),
      defaultValue: 0.00
    });

    await queryInterface.addColumn('SystemProfits', 'customerFee', {
      type: Sequelize.DECIMAL(20, 2),
      defaultValue: 0.00
    });

    await queryInterface.addColumn('SystemProfits', 'serviceCategory', {
      type: Sequelize.STRING(50),
      allowNull: true
    });

    await queryInterface.addColumn('SystemProfits', 'marginStatus', {
      type: Sequelize.STRING(20),
      defaultValue: 'profit'
    });

    await queryInterface.addColumn('SystemProfits', 'metadata', {
      type: Sequelize.JSON,
      allowNull: true
    });
  },

  down: async (queryInterface, Sequelize) => {
    // Revert the changes
    await queryInterface.removeColumn('SystemProfits', 'metadata');
    await queryInterface.removeColumn('SystemProfits', 'marginStatus');
    await queryInterface.removeColumn('SystemProfits', 'serviceCategory');
    await queryInterface.removeColumn('SystemProfits', 'customerFee');
    await queryInterface.removeColumn('SystemProfits', 'providerFee');
    await queryInterface.removeColumn('SystemProfits', 'currency');
    await queryInterface.removeColumn('SystemProfits', 'marginProfit');
    await queryInterface.removeColumn('SystemProfits', 'feeProfit');
    await queryInterface.removeColumn('SystemProfits', 'customerId');

    // Revert 'source' to ENUM
    await queryInterface.changeColumn('SystemProfits', 'source', {
      type: Sequelize.ENUM('deposit_fee', 'payout_fee'),
      allowNull: false
    });
  }
};
