import { QueryInterface, DataTypes } from 'sequelize';

/**
 * Migration to add TAT alert tracking fields to approval_levels table
 * These fields track whether TAT notifications have been sent
 */
export async function up(queryInterface: QueryInterface): Promise<void> {
  // Check and add columns only if they don't exist
  const tableDescription = await queryInterface.describeTable('approval_levels');
  
  if (!tableDescription.tat50_alert_sent) {
    await queryInterface.addColumn('approval_levels', 'tat50_alert_sent', {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    });
  }

  if (!tableDescription.tat75_alert_sent) {
    await queryInterface.addColumn('approval_levels', 'tat75_alert_sent', {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    });
  }

  if (!tableDescription.tat_breached) {
    await queryInterface.addColumn('approval_levels', 'tat_breached', {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    });
  }

  if (!tableDescription.tat_start_time) {
    await queryInterface.addColumn('approval_levels', 'tat_start_time', {
      type: DataTypes.DATE,
      allowNull: true
    });
  }
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.removeColumn('approval_levels', 'tat50_alert_sent');
  await queryInterface.removeColumn('approval_levels', 'tat75_alert_sent');
  await queryInterface.removeColumn('approval_levels', 'tat_breached');
  await queryInterface.removeColumn('approval_levels', 'tat_start_time');
}

