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 a contributor responsible for providing form-related mapping logic for entities.
/// Implements the <see cref="IEntityMapContributor"/> interface to participate in the entity mapping process.
/// </summary>
public class FormMapContributor : IEntityMapContributor
{
    /// <summary>
    /// Registers BSON class maps for <see cref="FormConfig"/> and <see cref="FormItemOverview"/> types used during MongoDB serialization, configuring default values and conditional serialization rules for their members. Each class map is only registered if it has not already been registered, preventing duplicate registrations.
    /// </summary>
    public void RegisterMaps()
        {
            if (!BsonClassMap.IsClassMapRegistered(typeof(FormConfig)))
                BsonClassMap.RegisterClassMap<FormConfig>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.Admission).SetDefaultValue(new FormItemOverview());
                    cm.MapMember(c => c.Demographic).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Discharge).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.IncomeInfo).SetIgnoreIfNull(true);
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(FormItemOverview)))
                BsonClassMap.RegisterClassMap<FormItemOverview>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.Nhc).SetDefaultValue(true);
                    cm.MapMember(c => c.Bed).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Name).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.LastName).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.SecondName).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Genre).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Birthday).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Origin).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.OriginAux).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Diagnostic).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.DiagnosticAux).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Allergy).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Language).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Insulation).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Service).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Destination).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.DestinationAux).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.AdmDischarge).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.NurseDischarge).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.MedicalDischarge).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.IncomingDate).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.UciDays).SetIgnoreIfNull(true);
                });
        }
}