import { type QueryInterface } from 'sequelize';

async function columnExists(
  queryInterface: QueryInterface,
  table: string,
  column: string,
): Promise<boolean> {
  const [results] = await queryInterface.sequelize.query(`
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public' AND table_name = '${table}' AND column_name = '${column}'
  `);
  return (results as unknown[]).length > 0;
}

export default {
  up: async (queryInterface: QueryInterface) => {
    if (await columnExists(queryInterface, 'users', 'importHash')) {
      await queryInterface.removeColumn('users', 'importHash');
    }
  },

  down: async () => {
    // Pre-launch cleanup: users are created through auth/provisioning, not import hashes.
  },
};
