import { QueryInterface, DataTypes } from 'sequelize';

/**
 * Migration: Add breach_reason column to approval_levels table
 * Purpose: Store TAT breach reason directly in approval_levels table
 * Date: 2025-11-18
 */

export async function up(queryInterface: QueryInterface): Promise<void> {
  // Check if table exists first
  const tables = await queryInterface.showAllTables();
  if (!tables.includes('approval_levels')) {
    // Table doesn't exist yet, skipping
    return;
  }

  // Get existing columns
  const tableDescription = await queryInterface.describeTable('approval_levels');

  // Add breach_reason column only if it doesn't exist
  if (!tableDescription.breach_reason) {
    await queryInterface.addColumn('approval_levels', 'breach_reason', {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Reason for TAT breach - can contain paragraph-length text'
    });
    console.log('✅ Added breach_reason column to approval_levels table');
  } else {
    console.log('ℹ️  breach_reason column already exists, skipping');
  }
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  // Check if table exists
  const tables = await queryInterface.showAllTables();
  if (!tables.includes('approval_levels')) {
    return;
  }

  // Get existing columns
  const tableDescription = await queryInterface.describeTable('approval_levels');

  // Remove column only if it exists
  if (tableDescription.breach_reason) {
    await queryInterface.removeColumn('approval_levels', 'breach_reason');
    console.log('✅ Removed breach_reason column from approval_levels table');
  }
}

