using Baya.Application.Contracts.Common;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace Baya.Infrastructure.Persistence.ValueConversion;

/// <summary>
/// Encrypts a string column at rest through the <see cref="IFieldEncryptor"/> seam. The ciphertext is
/// non-deterministic (random IV), so encrypted columns can never be equality-queried — lookups go
/// through a deterministic companion hash column (e.g. <c>PhoneHash</c>) instead.
/// </summary>
internal sealed class EncryptedStringConverter(IFieldEncryptor fieldEncryptor)
    : ValueConverter<string, string>(
        plaintext => fieldEncryptor.Encrypt(plaintext),
        ciphertext => fieldEncryptor.Decrypt(ciphertext));