

const require_uuid = require('../../../../utils/uuid.js');
const require_DidRepository = require('../../../../modules/dids/repository/DidRepository.js');
require('../../../../modules/dids/index.js');

//#region src/storage/migration/updates/0.3-0.3.1/did.ts
/**
* Migrates the {@link DidRecord} to 0.3 compatible format. It fetches all records from storage
* and applies the needed updates to the records. After a record has been transformed, it is updated
* in storage and the next record will be transformed.
*
* The following transformations are applied:
*  - {@link extractDidAsSeparateProperty}
*/
async function migrateDidRecordToV0_3_1(agent) {
	agent.config.logger.info("Migrating did records to storage version 0.3.1");
	const didRepository = agent.dependencyManager.resolve(require_DidRepository.DidRepository);
	agent.config.logger.debug("Fetching all did records from storage");
	const allDids = await didRepository.getAll(agent.context);
	agent.config.logger.debug(`Found a total of ${allDids.length} did records to update.`);
	for (const didRecord of allDids) {
		agent.config.logger.debug(`Migrating did record with id ${didRecord.id} to storage version 0.3.1`);
		const newId = require_uuid.uuid();
		agent.config.logger.debug(`Updating id ${didRecord.id} to ${newId} for did record`);
		didRecord.did = didRecord.id;
		didRecord.id = newId;
		await didRepository.save(agent.context, didRecord);
		await didRepository.deleteById(agent.context, didRecord.did);
		agent.config.logger.debug(`Successfully migrated did record with old id ${didRecord.did} to new id ${didRecord.id} to storage version 0.3.1`);
	}
}

//#endregion
exports.migrateDidRecordToV0_3_1 = migrateDidRecordToV0_3_1;