import { QueryInterface } from 'sequelize';

/**
 * Migration to add 'PAUSED' value to enum_workflow_status enum type
 * This allows workflows to have a PAUSED status in addition to the isPaused boolean flag
 */
export async function up(queryInterface: QueryInterface): Promise<void> {
  // Add 'PAUSED' to the enum_workflow_status enum type
  // PostgreSQL doesn't support IF NOT EXISTS for ALTER TYPE ADD VALUE,
  // so we check if it exists first
  await queryInterface.sequelize.query(`
    DO $$
    BEGIN
      IF NOT EXISTS (
        SELECT 1 FROM pg_enum 
        WHERE enumlabel = 'PAUSED' 
        AND enumtypid = (SELECT oid FROM pg_type WHERE typname = 'enum_workflow_status')
      ) THEN
        ALTER TYPE enum_workflow_status ADD VALUE 'PAUSED';
      END IF;
    END$$;
  `);
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  // Note: PostgreSQL doesn't support removing enum values directly
  // To fully rollback, you would need to:
  // 1. Create a new enum without 'PAUSED'
  // 2. Update all columns to use the new enum
  // 3. Drop the old enum
  // This is complex and risky, so we'll leave 'PAUSED' in the enum
  // even after rollback. This is a limitation of PostgreSQL enums.
  console.log('[Migration] Note: Cannot remove enum values in PostgreSQL. PAUSED will remain in enum_workflow_status.');
}

