'use strict';

async function addColumnIfNotExists(queryInterface, tableName, columnName, columnDefinition) {
  const tableDescription = await queryInterface.describeTable(tableName);
  if (!tableDescription[columnName]) {
    await queryInterface.addColumn(tableName, columnName, columnDefinition);
  }
}

module.exports = {
  async up(queryInterface, Sequelize) {
    // Add type column to wda table to distinguish WDA from DFStreamer uploads
    await addColumnIfNotExists(queryInterface, 'wda', 'type', {
      type: Sequelize.STRING,
      allowNull: false,
      defaultValue: 'wda',
      comment: 'Type of upload: wda or dfstreamer',
    });

    // Backfill: rows with filename starting with "DFStreamer" → set type to 'dfstreamer'
    await queryInterface.sequelize.query(`
      UPDATE wda SET type = 'dfstreamer'
      WHERE filename LIKE 'DFStreamer%' AND type = 'wda';
    `);
  },

  async down(queryInterface) {
    await queryInterface.removeColumn('wda', 'type');
  },
};
