import { type QueryInterface } from 'sequelize';

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

export default {
  up: async (queryInterface: QueryInterface) => {
    if (await tableExists(queryInterface, 'staff')) {
      await queryInterface.dropTable('staff');
    }

    await queryInterface.sequelize.query(
      'DROP TYPE IF EXISTS "public"."enum_staff_staff_type"',
    );
    await queryInterface.sequelize.query(
      'DROP TYPE IF EXISTS "public"."enum_staff_status"',
    );
  },

  down: async (queryInterface: QueryInterface) => {
    void queryInterface;
  },
};
