import { type QueryInterface } from 'sequelize';

const DROPPED_USER_COLUMNS = ['job_title', 'hire_date'] as const;

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) => {
    for (const column of DROPPED_USER_COLUMNS) {
      if (await columnExists(queryInterface, 'users', column)) {
        await queryInterface.removeColumn('users', column);
      }
    }
  },

  down: async () => {
    // Pre-launch cleanup: role + scope are the canonical user classification.
  },
};
