'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    const table = 'production_qc';
    const cols = await queryInterface.describeTable(table);

    if (!cols.qc_by) {
      await queryInterface.addColumn(table, 'qc_by', {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        after: 'moved_to_qc_by'
      });
    }

    if (!cols.qc_at) {
      await queryInterface.addColumn(table, 'qc_at', {
        type: Sequelize.DATE,
        allowNull: true,
        after: 'qc_by'
      });
    }
  },

  async down(queryInterface) {
    const table = 'production_qc';
    const cols = await queryInterface.describeTable(table);

    if (cols.qc_at) {
      await queryInterface.removeColumn(table, 'qc_at');
    }
    if (cols.qc_by) {
      await queryInterface.removeColumn(table, 'qc_by');
    }
  }
};
