using MongoDB.Bson;
using MongoDB.Driver;
using MongoMigrations.Core;

namespace adas_core.Infrastructure.Migrations.MongoMigrations;

/// <summary>
/// Represents a database migration responsible for updating the display configuration related to a driver in version 0.1.1.
/// </summary>
/// <remarks>
/// Inherits from <see cref="Migration"/> to support schema or data changes associated with the 0.1.1 release.
/// </remarks>
public class U_0_1_1_UpdateDisplayConfigDriver : Migration
{
    public U_0_1_1_UpdateDisplayConfigDriver() : base(11)
    {
        Description =
            "Genera el campo Type en todos los documentos de la coleccion DisplayConfig por que el nuevo driver no expone _t para polimorfia";
    }

    /// <summary>
    /// Updates documents in the <c>config_displays</c> collection by mapping the <c>_t</c> discriminator field
    /// to a new <c>type</c> field, normalizing values to <c>DisplayNurse</c>, <c>StandarDisplay</c>, or <c>SmartDisplay</c>
    /// (defaulting to <c>Unknown</c> for any other value), removing the original <c>_t</c> field, and merging the
    /// results back into the same collection while discarding documents that do not already have a match.
    /// </summary>
    public override void Update()
        {
            var collection = Database.GetCollection<BsonDocument>("config_displays");
    
            var pipeline = new[]
            {
                new BsonDocument("$set", new BsonDocument
                {
                    {
                        "type", new BsonDocument("$switch", new BsonDocument
                        {
                            {
                                "branches", new BsonArray
                                {
                                    new BsonDocument
                                    {
                                        { "case", new BsonDocument("$eq", new BsonArray { "$_t", "DisplayNurse" }) },
                                        { "then", "DisplayNurse" }
                                    },
                                    new BsonDocument
                                    {
                                        { "case", new BsonDocument("$eq", new BsonArray { "$_t", "StandarDisplay" }) },
                                        { "then", "StandarDisplay" }
                                    },
                                    new BsonDocument
                                    {
                                        { "case", new BsonDocument("$eq", new BsonArray { "$_t", "SmartDisplay" }) },
                                        { "then", "SmartDisplay" }
                                    }
                                }
                            },
                            { "default", "Unknown" }
                        })
                    }
                }),
    
                new BsonDocument("$unset", "_t"),
    
                new BsonDocument("$merge", new BsonDocument
                {
                    { "into", "config_displays" },
                    { "whenMatched", "merge" },
                    { "whenNotMatched", "discard" }
                })
            };
    
            collection.AggregateToCollection<BsonDocument>(pipeline);
            // collection.AggregateToCollection<BsonDocument>(pipeline, "config_displays");
    
        }

}