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>
/// Provides mapping contributions for the <c>BannerConfig</c> entity by implementing the <see cref="IEntityMapContributor"/> contract.
/// </summary>
/// <remarks>
/// Used as a contributor that participates in entity map configuration for banner-related data.
/// </remarks>
public class BannerConfigMapContributor : IEntityMapContributor
{
    /// <summary>
    /// Registers BSON class maps for banner-related configuration types, including <see cref="BannerItem"/>, <see cref="BannerItemConfig"/>, <see cref="BannerItemTableConfig"/>, and <see cref="HeaderBannerItemTableConfig"/>. Each map is only registered when no existing registration is found, and applies custom serialization (enum values stored as strings), null-ignoring behavior for optional members, and default values where appropriate.
    /// </summary>
    public void RegisterMaps()
        {
            if (!BsonClassMap.IsClassMapRegistered(typeof(BannerItem)))
                BsonClassMap.RegisterClassMap<BannerItem>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.Type)
                        .SetIgnoreIfNull(true)
                        .SetSerializer(
                            new NullableSerializer<DisplayConfigEnums.BannerType>(
                                new EnumSerializer<DisplayConfigEnums.BannerType>(BsonType.String)));
                    cm.MapMember(c => c.GrowPriority).SetDefaultValue(1);
                    cm.MapMember(c => c.Config).SetIgnoreIfNull(true);
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(BannerItemConfig)))
                BsonClassMap.RegisterClassMap<BannerItemConfig>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.BannerItemDialogTableConfig).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.BannerItemTableConfig).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.MedicalStaffConfig).SetIgnoreIfNull(true);
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(BannerItemTableConfig)))
                BsonClassMap.RegisterClassMap<BannerItemTableConfig>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.Config).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.BgColor).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.TextColor).SetIgnoreIfNull(true);
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(HeaderBannerItemTableConfig)))
                BsonClassMap.RegisterClassMap<HeaderBannerItemTableConfig>(cm =>
                {
                    cm.MapMember(c => c.Type)
                        .SetIgnoreIfNull(true)
                        .SetSerializer(
                            new NullableSerializer<DisplayConfigEnums.CellType>(
                                new EnumSerializer<DisplayConfigEnums.CellType>(BsonType.String)));
                    cm.MapMember(c => c.SubType).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Field).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.GrowPriority).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Icon).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.BgColor).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.TextColor).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Title).SetIgnoreIfNull(true);
                });
        }
}