'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.addColumn('material_plant', 'opening_balance', {
      type: Sequelize.DECIMAL(18, 3),
      allowNull: true,
      defaultValue: 0.000,
      comment: 'Opening stock balance for this material at this plant'
    });

    await queryInterface.addColumn('material_plant', 'opening_balance_date', {
      type: Sequelize.DATEONLY,
      allowNull: true,
      comment: 'Date when opening balance was set'
    });

    await queryInterface.addColumn('material_plant', 'opening_balance_uom_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: {
        model: 'uom',
        key: 'id'
      },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL',
      comment: 'UOM for opening balance (defaults to material base UOM)'
    });

    await queryInterface.addColumn('material_plant', 'opening_balance_unit_cost', {
      type: Sequelize.DECIMAL(18, 2),
      allowNull: true,
      defaultValue: 0.00,
      comment: 'Unit cost for opening balance valuation'
    });
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.removeColumn('material_plant', 'opening_balance_unit_cost');
    await queryInterface.removeColumn('material_plant', 'opening_balance_uom_id');
    await queryInterface.removeColumn('material_plant', 'opening_balance_date');
    await queryInterface.removeColumn('material_plant', 'opening_balance');
  }
};

