import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { CryptoUtil } from '@it0/common';

@Injectable()
export class CredentialVaultService {
  private readonly masterKey: string;

  constructor(private readonly configService: ConfigService) {
    this.masterKey = this.configService.get<string>('VAULT_MASTER_KEY', 'dev-master-key');
  }

  encrypt(plaintext: string): { encrypted: Buffer; iv: Buffer } {
    return CryptoUtil.encrypt(plaintext, this.masterKey);
  }

  decrypt(encrypted: Buffer, iv: Buffer): string {
    return CryptoUtil.decrypt(encrypted, iv, this.masterKey);
  }
}
