using adas_core.Domain.Models.MongoModels;
using adas_core.Infrastructure.Utils.MongoMaps.Interfaz;
using MongoDB.Bson.Serialization;
using Stream = System.IO.Stream;

namespace adas_core.Infrastructure.Utils.MongoMaps;

/// <summary>
/// Represents a contributor that participates in entity mapping operations related to camera entities.
/// </summary>
/// <remarks>
/// Implements the <see cref="IEntityMapContributor"/> interface to provide camera-specific mapping behavior within the entity mapping pipeline.
/// </remarks>
public class CameraContributor : IEntityMapContributor
{
    /// <summary>
    /// Registers BSON class maps for the <see cref="Camera"/> and <see cref="Domain.Models.MongoModels.Stream"/> types with MongoDB, applying custom mapping conventions such as element name overrides, default values, and ignore-if-null behavior. Registration is performed only once; the method guards against duplicate registration by checking <see cref="BsonClassMap.IsClassMapRegistered(Type)"/> before registering.
    /// </summary>
    public void RegisterMaps()
        {
            if (!BsonClassMap.IsClassMapRegistered(typeof(Camera)))
            {
                BsonClassMap.RegisterClassMap<Camera>(cm =>
                {
                    cm.AutoMap();
                    cm.MapIdMember(c => c.Id).SetElementName("_id");
                    cm.MapMember(c => c.Streams).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Name).SetDefaultValue(string.Empty);
                    cm.MapMember(c => c.Ptz).SetDefaultValue(false);
                    cm.MapMember(c => c.Driver).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Ip).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Username).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Password).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.InUse).SetIgnoreIfNull(true).SetIsRequired(false);
                });
    
                BsonClassMap.RegisterClassMap<Domain.Models.MongoModels.Stream>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.Rtsp).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Jpeg).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.WebRtc).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Hls).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Mp4).SetIgnoreIfNull(true);
                });
            }
        }
}