using Knot.Modules.Stories.Domain;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;

namespace Knot.Modules.Stories.Infrastructure.Persistence.Mongo;

public static class StoriesMongoMapConfigurator
{
    private static bool _configured;

    public static void Configure()
    {
        if (_configured) return;

        try { BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard)); } catch { /* Already registered */ }

        BsonClassMap.RegisterClassMap<Story>(cm =>
        {
            cm.AutoMap();
            // Encrypt Content and MediaUrl
            cm.MapProperty(c => c.Content).SetSerializer(new EncryptedStringSerializer());
            cm.MapProperty(c => c.MediaUrl).SetSerializer(new EncryptedStringSerializer());
        });

        _configured = true;
    }
}
