'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    const materialTable = await queryInterface.describeTable('material');
    if (!materialTable.unit_cost) {
      await queryInterface.addColumn('material', 'unit_cost', {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true,
        defaultValue: 0.000,
        comment: 'Material unit cost for BOM/Pricing defaults',
        after: 'net_weight',
      });
    }

    const bomComponentTable = await queryInterface.describeTable('bom_component');
    if (!bomComponentTable.rate) {
      await queryInterface.addColumn('bom_component', 'rate', {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true,
        defaultValue: 0.000,
        comment: 'Unit rate auto-fetched from material master',
        after: 'component_uom_id',
      });
    }
  },

  async down(queryInterface) {
    const bomComponentTable = await queryInterface.describeTable('bom_component');
    if (bomComponentTable.rate) {
      await queryInterface.removeColumn('bom_component', 'rate');
    }

    const materialTable = await queryInterface.describeTable('material');
    if (materialTable.unit_cost) {
      await queryInterface.removeColumn('material', 'unit_cost');
    }
  },
};
