'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Add stores-specific fields to material_issue table
    await queryInterface.addColumn('material_issue', 'issued_by', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: {
        model: 'user',
        key: 'id'
      },
      comment: 'User who issued the materials (stores person)'
    });

    await queryInterface.addColumn('material_issue', 'issued_to', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: {
        model: 'user',
        key: 'id'
      },
      comment: 'User who received the materials (production person)'
    });

    await queryInterface.addColumn('material_issue', 'material_request_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: {
        model: 'material_request',
        key: 'id'
      },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL',
      comment: 'Link to Material Request if issued from MR'
    });

    await queryInterface.addIndex('material_issue', ['issued_by']);
    await queryInterface.addIndex('material_issue', ['issued_to']);
    await queryInterface.addIndex('material_issue', ['material_request_id']);
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.removeIndex('material_issue', ['material_request_id']);
    await queryInterface.removeIndex('material_issue', ['issued_to']);
    await queryInterface.removeIndex('material_issue', ['issued_by']);
    
    await queryInterface.removeColumn('material_issue', 'material_request_id');
    await queryInterface.removeColumn('material_issue', 'issued_to');
    await queryInterface.removeColumn('material_issue', 'issued_by');
  }
};

