using adas_core.Domain.Models.MongoModels;
using adas_core.Infrastructure.Utils.MongoMaps.Interfaz;
using adas_core.module.LightBeacons.Devices;
using MongoDB.Bson.Serialization;
using LightBeacon = adas_core.Domain.Models.MongoModels.LightBeacon;

namespace adas_core.Infrastructure.Utils.MongoMaps;

/// <summary>
/// Represents a map contributor that participates in entity mapping operations
/// for beacon-related entities.
/// </summary>
/// <remarks>
/// Implements the <see cref="IEntityMapContributor"/> interface to contribute
/// beacon-specific mapping logic.
/// </remarks>
public class BeaconMapContributor : IEntityMapContributor
{
    /// <summary>
    /// Registers BSON class maps for <see cref="LightBeacon"/>, <see cref="Options"/>, and <see cref="LightBeaconAbstract"/> types to configure their MongoDB serialization. Each registration is skipped if a class map for the type has already been registered, applying default values, element name mappings, and ignore-if-null settings to the respective members.
    /// </summary>
    public void RegisterMaps()
        {
            if (!BsonClassMap.IsClassMapRegistered(typeof(LightBeacon)))
                BsonClassMap.RegisterClassMap<LightBeacon>(cm =>
                {
                    cm.AutoMap();
                    cm.MapIdMember(c => c.Id).SetElementName("_id");
                    cm.MapMember(c => c.Type).SetDefaultValue(string.Empty);
                    cm.MapMember(c => c.Name).SetDefaultValue(string.Empty);
                    cm.MapMember(c => c.Options).SetDefaultValue(new Options());
                    cm.MapMember(c => c.InUse).SetIgnoreIfNull(true).SetIsRequired(false);
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(Options)))
                BsonClassMap.RegisterClassMap<Options>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.Url).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Port).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Password).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Emulate).SetDefaultValue(false);
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(LightBeaconAbstract)))
                BsonClassMap.RegisterClassMap<LightBeaconAbstract>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.Host);
                    cm.MapMember(c => c.Password);
                });
        }
}