'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    const table = await queryInterface.describeTable('fg_stock_ledger');
    if (!Object.prototype.hasOwnProperty.call(table, 'stock_ledger_no')) {
      await queryInterface.addColumn('fg_stock_ledger', 'stock_ledger_no', {
        type: Sequelize.STRING(60),
        allowNull: true
      });
    }

    const indexes = await queryInterface.showIndex('fg_stock_ledger');
    const hasIndex = indexes.some((idx) => idx.name === 'idx_fg_stock_ledger_no');
    if (!hasIndex) {
      await queryInterface.addIndex('fg_stock_ledger', ['stock_ledger_no'], {
        name: 'idx_fg_stock_ledger_no',
        unique: true
      });
    }
  },

  async down(queryInterface) {
    const indexes = await queryInterface.showIndex('fg_stock_ledger');
    const hasIndex = indexes.some((idx) => idx.name === 'idx_fg_stock_ledger_no');
    if (hasIndex) {
      await queryInterface.removeIndex('fg_stock_ledger', 'idx_fg_stock_ledger_no');
    }

    const table = await queryInterface.describeTable('fg_stock_ledger');
    if (Object.prototype.hasOwnProperty.call(table, 'stock_ledger_no')) {
      await queryInterface.removeColumn('fg_stock_ledger', 'stock_ledger_no');
    }
  }
};
