'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Add new column material_group_id
    await queryInterface.addColumn('material', 'material_group_id', {
      type: Sequelize.INTEGER,
      allowNull: true, // Temporarily allow null, will be populated by data migration
      references: {
        model: 'material_group',
        key: 'id'
      },
      onUpdate: 'CASCADE',
      onDelete: 'RESTRICT',
      comment: 'Foreign key to MaterialGroup master'
    });

    // Make existing material_group column nullable (it's already nullable, but ensure it)
    await queryInterface.changeColumn('material', 'material_group', {
      type: Sequelize.STRING(50),
      allowNull: true // Allow null for backward compatibility after migration
    });

    await queryInterface.addIndex('material', ['material_group_id'], { name: 'material_material_group_id_idx' });
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.removeIndex('material', 'material_material_group_id_idx');
    await queryInterface.removeColumn('material', 'material_group_id');
  }
};

