import { Logger } from '@n8n/backend-common';
import { Service } from '@n8n/di';
import { EncryptionKeyProxy, InstanceSettings } from 'n8n-core';

import { KeyManagerService } from './key-manager.service';

@Service()
export class EncryptionBootstrapService {
	constructor(
		private readonly keyManager: KeyManagerService,
		private readonly instanceSettings: InstanceSettings,
		private readonly encryptionKeyProxy: EncryptionKeyProxy,
		private readonly logger: Logger,
	) {
		this.logger = this.logger.scoped('encryption-key-manager');
	}

	async run(): Promise<void> {
		if (this.instanceSettings.instanceType === 'main') {
			await this.keyManager.bootstrapLegacyCbcKey(this.instanceSettings.encryptionKey);
			await this.keyManager.bootstrapGcmKey();
		}
		this.encryptionKeyProxy.setProvider(this.keyManager);
		this.logger.debug('Encryption key bootstrap complete');
	}
}
