using IRaCIS.Core.Application.MassTransit.Consumer;
using IRaCIS.Core.Domain.BaseModel;
using MassTransit;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace IRaCIS.Core.API
{
    public static class MassTransitSetup
    {
        public static void AddMassTransitSetup(this IServiceCollection services)
        {

            #region MassTransit
            //masstransit组件 也支持MediatR 中介者模式,但是支持分布式,考虑后续,所以在次替代MediatR
            //参考链接:https://masstransit.io/documentation/concepts/mediator#scoped-mediator
            services.AddMediator(cfg =>
            {
                cfg.AddConsumers(typeof(UserSiteSurveySubmitedEventConsumer).Assembly);

                //cfg.AddConsumer<ConsistencyCheckConsumer>();
                //cfg.AddConsumer<AddSubjectTriggerConsumer>();
                //cfg.AddConsumer<AddSubjectTriggerConsumer2>();
                //cfg.ConfigureMediator((context, cfg) => cfg.UseHttpContextScopeFilter(context));
            });

            //添加 MassTransit 和 InMemory 传输
            services.AddMassTransit(cfg =>
            {
                cfg.AddConsumers(typeof(UserSiteSurveySubmitedEventConsumer).Assembly);

                cfg.AddPublishMessageScheduler();
                cfg.AddHangfireConsumers();

                // 使用 InMemory 作为消息传递机制
                cfg.UsingInMemory((context, cfg) =>
                {
                    cfg.UsePublishMessageScheduler();


                    cfg.UseConsumeFilter(typeof(ConsumeExceptionFilter<>), context,
                        x => x.Include(type => type.IsAssignableTo(typeof(DomainEvent))));
                    
                    cfg.UseConsumeFilter(typeof(CultureInfoFilter<>), context,
                          x => x.Include(type => type.IsAssignableTo(typeof(DomainEvent))));

                    cfg.ConfigureEndpoints(context); // 自动配置所有消费者的端点


                });
            });

            #endregion


        }
    }
}