'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Add column first WITHOUT FK reference to avoid Sequelize generating
    // an auto FK index name that can exceed MySQL's 64-char identifier limit.
    await queryInterface.addColumn('production_confirmation', 'production_order_routing_execution_id', {
      type: Sequelize.INTEGER,
      allowNull: true
    });

    await queryInterface.addIndex('production_confirmation', ['production_order_routing_execution_id'], {
      name: 'production_confirmation_po_routing_exec_id_idx'
    });

    // Add FK constraint with a short explicit name
    await queryInterface.addConstraint('production_confirmation', {
      fields: ['production_order_routing_execution_id'],
      type: 'foreign key',
      name: 'pc_po_routing_exec_fk',
      references: {
        table: 'production_order_routing_execution',
        field: 'id'
      },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL'
    });
  },

  async down(queryInterface, Sequelize) {
    // Remove FK constraint first
    await queryInterface.removeConstraint('production_confirmation', 'pc_po_routing_exec_fk');
    await queryInterface.removeIndex('production_confirmation', 'production_confirmation_po_routing_exec_id_idx');
    await queryInterface.removeColumn('production_confirmation', 'production_order_routing_execution_id');
  }
};

