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

namespace adas_core.Infrastructure.Migrations.MongoMigrations;

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";
    }

    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");

    }

}