'use strict';

/** MySQL max identifier length 64 — avoid auto FK name route_card_operation_material_consumption_work_order_id_... */

const FK_RCOMC_WORK_ORDER = 'fk_rcomc_work_order';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.addColumn('route_card_operation_material_consumption', 'work_order_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      comment: 'Work order on the route card (denormalized for operation-wise consumption reporting)',
    });
    await queryInterface.addConstraint('route_card_operation_material_consumption', {
      fields: ['work_order_id'],
      type: 'foreign key',
      name: FK_RCOMC_WORK_ORDER,
      references: { table: 'work_order', field: 'id' },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL',
    });
    await queryInterface.addColumn('route_card_operation_material_consumption', 'wip_material_class', {
      type: Sequelize.STRING(20),
      allowNull: true,
      comment: 'RAW_MATERIAL | FASTENER at save (audit)',
    });
  },

  async down(queryInterface) {
    await queryInterface.removeColumn('route_card_operation_material_consumption', 'wip_material_class');
    await queryInterface.removeConstraint('route_card_operation_material_consumption', FK_RCOMC_WORK_ORDER);
    await queryInterface.removeColumn('route_card_operation_material_consumption', 'work_order_id');
  },
};
