'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up (queryInterface, Sequelize) {
    // Add department_id column to material_request table
    await queryInterface.addColumn('material_request', 'department_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: {
        model: 'department',
        key: 'id'
      },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL',
      comment: 'Department that requested the materials'
    });

    // Add index for department_id for better query performance
    await queryInterface.addIndex('material_request', ['department_id'], {
      name: 'material_request_department_id_idx'
    });
  },

  async down (queryInterface, Sequelize) {
    // Remove index first
    await queryInterface.removeIndex('material_request', 'material_request_department_id_idx');
    
    // Remove department_id column
    await queryInterface.removeColumn('material_request', 'department_id');
  }
};
