using adas_core.Domain.Enums;
using adas_core.Domain.Models.MongoModels;
using adas_core.Infrastructure.Utils.MongoMaps.Interfaz;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;

namespace adas_core.Infrastructure.Utils.MongoMaps;

/// <summary>
/// Represents a contributor that supplies entity map configuration related to display home functionality.
/// </summary>
public class DisplayHomeConfigContributor : IEntityMapContributor
{
    /// <summary>
    /// Registers BSON class maps for <see cref="CardConfig"/> and <see cref="RowCardConfig"/> types if they are not already registered, configuring MongoDB serialization with custom element names, default values, null-ignore behavior, and string-based enum serialization.
    /// </summary>
    public void RegisterMaps()
        {
            if (!BsonClassMap.IsClassMapRegistered(typeof(CardConfig)))
                BsonClassMap.RegisterClassMap<CardConfig>(cm =>
                {
                    cm.AutoMap();
                    cm.MapIdMember(c => c.Id).SetElementName("_id");
                    cm.MapMember(c => c.Rows).SetDefaultValue(new List<RowCardConfig>());
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(RowCardConfig)))
                BsonClassMap.RegisterClassMap<RowCardConfig>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.Cells).SetDefaultValue(new List<Cell>());
                    cm.MapMember(c => c.GrowPriority).SetDefaultValue(1);
                    cm.MapMember(c => c.Border).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.BorderRadius).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Title).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Size).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.MarginTop).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.MarginBottom).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.MarginLeft).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.MarginRight).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.BgColor).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.PaddingTop).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.PaddingBottom).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.PaddingLeft).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.PaddingRight).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Type)
                        .SetDefaultValue(DisplayConfigEnums.CellType.Default)
                        .SetSerializer(new EnumSerializer<DisplayConfigEnums.CellType>(BsonType.String));
                });
        }
}