import {MigrationCallback} from 'realm';
import DBSchemas from './schemas';
import {databaseConf, databaseNames} from './types';

export const DBMigration: {[key in databaseNames]: any} = {
  users: (Schema: databaseConf<typeof DBSchemas.users.defaultProps>) => {
    const callback: MigrationCallback = (oldRealm, newRealm) => {
      /*// only apply this change if upgrading to schemaVersion 2
        if (oldRealm.schemaVersion < 2) {
          const oldObjects = oldRealm.objects('Person');
          const newObjects = newRealm.objects('Person');
          // loop through all objects and set the fullName property in the new schema
          for (const objectIndex in oldObjects) {
            const oldObject = oldObjects[objectIndex];
            const newObject = newObjects[objectIndex];
            newObject.fullName = `${oldObject.firstName} ${oldObject.lastName}`;
          }
        }*/
    };

    return callback;
  },
  chat: (Schema: databaseConf<typeof DBSchemas.chat.defaultProps>) => {
    const callback: MigrationCallback = (oldRealm, newRealm) => {
      /*// only apply this change if upgrading to schemaVersion 2
        if (oldRealm.schemaVersion < 2) {
          const oldObjects = oldRealm.objects('Person');
          const newObjects = newRealm.objects('Person');
          // loop through all objects and set the fullName property in the new schema
          for (const objectIndex in oldObjects) {
            const oldObject = oldObjects[objectIndex];
            const newObject = newObjects[objectIndex];
            newObject.fullName = `${oldObject.firstName} ${oldObject.lastName}`;
          }
        }*/
    };

    return callback;
  },
};
