'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    const table = 'production_final_inspection';
    const tables = await queryInterface.showAllTables();
    if (!tables.includes(table)) return;

    const desc = await queryInterface.describeTable(table);
    if (!desc.rework_from_operation_id) {
      await queryInterface.addColumn(table, 'rework_from_operation_id', {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'route_card_operation', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      });
    }
  },

  async down(queryInterface) {
    const table = 'production_final_inspection';
    const tables = await queryInterface.showAllTables();
    if (!tables.includes(table)) return;

    const desc = await queryInterface.describeTable(table);
    if (desc.rework_from_operation_id) {
      await queryInterface.removeColumn(table, 'rework_from_operation_id');
    }
  }
};
