using adas_core.Domain.Enums;
using adas_core.Domain.Models.MongoModels;
using adas_core.Domain.Utils;
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>
/// Represents a contributor that provides device-related mapping logic for entities.
/// Implements the <see cref="IEntityMapContributor"/> interface to participate in the entity mapping process.
/// </summary>
public class DeviceMapContributor : IEntityMapContributor
{
    /// <summary>
    /// Registers BSON class maps for <see cref="DeviceActionType"/>, <see cref="DeviceAction"/>, <see cref="DeviceSettings"/>, and <see cref="Device"/> types used in MongoDB serialization.
    /// </summary>
    /// <remarks>
    /// Each map is only registered if no class map is already registered for the type. The registration applies auto-mapping and customizes serialization for enum members (stored as strings), applies default values for required properties, and configures nullable members to be ignored when null.
    /// </remarks>
    public void RegisterMaps()
        {
            if (!BsonClassMap.IsClassMapRegistered(typeof(DeviceActionType)))
            {
                BsonClassMap.RegisterClassMap<DeviceActionType>(cm => cm.AutoMap() );
            }
            
            if (!BsonClassMap.IsClassMapRegistered(typeof(DeviceAction)))
            {
                BsonClassMap.RegisterClassMap<DeviceAction>(cm =>
                    {
                        cm.AutoMap();
                        cm.MapMember(c => c.Type)
                            .SetDefaultValue(DeviceActionType.Unknown)
                            .SetSerializer(new EnumSerializer<DeviceActionType>(BsonType.String));
                        cm.MapMember(c => c.ConfigObservationId).SetIgnoreIfNull(true);
                        cm.MapMember(c => c.AlarmName).SetIgnoreIfNull(true);
                        cm.MapMember(c => c.ValueOnSingleClick)
                            .SetDefaultValue(new object())
                            .SetSerializer(new ComplexObjectValueTypeSerializer());
                        cm.MapMember(c => c.ValueOnDoubleClick)
                            .SetDefaultValue(new object())
                            .SetSerializer(new ComplexObjectValueTypeSerializer());
                        cm.MapMember(c => c.ValueOnHoldClick)
                            .SetDefaultValue(new object())
                            .SetSerializer(new ComplexObjectValueTypeSerializer());
                    }
                    );
            }
            
            if (!BsonClassMap.IsClassMapRegistered(typeof(DeviceSettings)))
            {
                BsonClassMap.RegisterClassMap<DeviceSettings>(cm =>
                    {
                        cm.AutoMap();
                        cm.MapMember(c => c.Action).SetDefaultValue(new DeviceAction());
                    }
                    );
            }
            
            if (!BsonClassMap.IsClassMapRegistered(typeof(Device)))
            {
                BsonClassMap.RegisterClassMap<Device>(cm =>
                    {
                        cm.AutoMap();
                        cm.MapIdMember(c => c.Id).SetElementName("_id");
                        cm.MapMember(c => c.DeviceType)
                            .SetDefaultValue(DeviceType.Unknown)
                            .SetSerializer(new EnumSerializer<DeviceType>(BsonType.String));
                        cm.MapMember(c => c.Uuid).SetIgnoreIfNull(true);
                        cm.MapMember(c => c.MacAddr).SetIgnoreIfNull(true);
                        cm.MapMember(c => c.CreatedAt).SetDefaultValue(DateTime.UtcNow);
                        cm.MapMember(c => c.UpdatedAt).SetDefaultValue(DateTime.UtcNow);
                        cm.MapMember(c => c.Key).SetIgnoreIfNull(true);
                        cm.MapMember(c => c.Color).SetIgnoreIfNull(true);
                        cm.MapMember(c => c.Battery).SetIgnoreIfNull(true);
                        cm.MapMember(c => c.Name).SetIgnoreIfNull(true);
                        cm.MapMember(c => c.SerialNumber).SetIgnoreIfNull(true);
                        cm.MapMember(c => c.PointOfCareIds).SetDefaultValue(new List<ObjectId>());
                        cm.MapMember(c => c.Connected).SetDefaultValue(false).SetIgnoreIfNull(true);
                        cm.MapMember(c => c.Ready).SetDefaultValue(false).SetIgnoreIfNull(true);
                        cm.MapMember(c => c.DeviceType)
                            .SetSerializer(new EnumSerializer<DeviceType>(BsonType.String));
                        cm.MapMember(c => c.Settings).SetDefaultValue(new DeviceSettings());
                    }
                    );
            }
        }
}