'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Add storage_location_id column to material table
    await queryInterface.addColumn('material', 'storage_location_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: {
        model: 'storage_location',
        key: 'id'
      },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL',
      comment: 'Default storage location for this material'
    });

    // Add is_default_storage_location column to material table
    await queryInterface.addColumn('material', 'is_default_storage_location', {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: 'Indicates if this is the default storage location for the material'
    });

    // Add index for storage_location_id for better query performance
    await queryInterface.addIndex('material', ['storage_location_id'], {
      name: 'idx_material_storage_location_id'
    });
  },

  async down(queryInterface, Sequelize) {
    // Remove index first
    await queryInterface.removeIndex('material', 'idx_material_storage_location_id');
    
    // Remove columns
    await queryInterface.removeColumn('material', 'is_default_storage_location');
    await queryInterface.removeColumn('material', 'storage_location_id');
  }
};
