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

namespace adas_core.Infrastructure.Utils.MongoMaps;

/// <summary>
/// Represents an entity map contributor that provides mapping configuration for service configuration entities.
/// </summary>
public class ServiceConfigMapContributor : IEntityMapContributor
{
    /// <summary>
    /// Registers BSON class maps for service configuration-related types, including <see cref="ServiceConfig"/>, <see cref="ServiceConfigService"/>, <see cref="ServiceConfigServiceSection"/>, <see cref="ServiceConfigTheme"/>, and <see cref="ServiceConfigThemeTimetable"/>, configuring their serialization elements, default values, and null-ignoring behavior. Each registration is guarded by a check that ensures the map is only registered if it has not been registered previously.
    /// </summary>
    public void RegisterMaps()
        {
            if (!BsonClassMap.IsClassMapRegistered(typeof(ServiceConfig)))
                BsonClassMap.RegisterClassMap<ServiceConfig>(cm =>
                {
                    cm.AutoMap();
                    cm.MapIdMember(c => c.Id).SetElementName("_id");
                    cm.MapMember(c => c.StrId).SetElementName("id")
                        .SetDefaultValue(string.Empty);
                    cm.MapMember(c => c.Service).SetDefaultValue(new List<ServiceConfigService>());
                    cm.MapMember(c => c.Theme).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.BoxObservations).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Score).SetDefaultValue(string.Empty);
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(ServiceConfigService)))
                BsonClassMap.RegisterClassMap<ServiceConfigService>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.Screen).SetDefaultValue(string.Empty);
                    cm.MapMember(c => c.Sections).SetDefaultValue(new List<ServiceConfigServiceSection>());
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(ServiceConfigServiceSection)))
                BsonClassMap.RegisterClassMap<ServiceConfigServiceSection>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.BoxId).SetDefaultValue(string.Empty);
                    cm.MapMember(c => c.Box).SetDefaultValue(string.Empty);
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(ServiceConfigTheme)))
                BsonClassMap.RegisterClassMap<ServiceConfigTheme>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.DefaultTheme).SetElementName("default")
                        .SetDefaultValue(string.Empty);
                    cm.MapMember(c => c.Timetables).SetDefaultValue(new List<ServiceConfigThemeTimetable>());
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(ServiceConfigThemeTimetable)))
                BsonClassMap.RegisterClassMap<ServiceConfigThemeTimetable>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.Theme).SetDefaultValue(string.Empty);
                    cm.MapMember(c => c.StartDay).SetDefaultValue(string.Empty);
                    cm.MapMember(c => c.EndDay).SetDefaultValue(string.Empty);
                    cm.MapMember(c => c.StartHour).SetDefaultValue(string.Empty);
                    cm.MapMember(c => c.EndHour).SetDefaultValue(string.Empty);
                });
        }
}