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', 'staff_type')) {
      await queryInterface.removeColumn('users', 'staff_type');
    }
    await queryInterface.sequelize.query(
      'DROP TYPE IF EXISTS "public"."enum_staff_staff_type"',
    );
  },

  down: async () => {
    // Pre-launch cleanup: staff classification is role + scope only.
  },
};
