'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Step 1: Add material_type_id column (nullable initially)
    await queryInterface.addColumn('material', 'material_type_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: {
        model: 'material_type',
        key: 'id'
      },
      onUpdate: 'CASCADE',
      onDelete: 'RESTRICT',
      comment: 'Reference to Material Type master'
    });

    // Step 2: Create a mapping of old ENUM values to new type codes
    // We'll need to create material types first, then map
    // This migration assumes material types will be created via seeder
    
    // Step 3: Add index
    await queryInterface.addIndex('material', ['material_type_id'], {
      name: 'idx_material_material_type_id'
    });

    // Note: Data migration from material_type ENUM to material_type_id
    // will be handled in a separate data migration script or seeder
  },

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

