using IRaCIS.Core.Application.Triggers;
using IRaCIS.Core.Infra.EFCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace IRaCIS.Core.API
{
    public static class EFSetup
    {
        public static void AddEFSetup( this IServiceCollection services, IConfiguration configuration)
        {
            //services.AddScoped<DbContext, IRaCISDBContext>();

            //这个注入没有成功--注入是没问题的，构造函数也只是支持参数就好，错在注入的地方不能写DbContext
            //Web程序中通过重用池中DbContext实例可提高高并发场景下的吞吐量， 这在概念上类似于ADO.NET Provider原生的连接池操作方式，具有节省DbContext实例化成本的优点
            services.AddDbContext<IRaCISDBContext>(options =>
            {
                options.UseSqlServer(configuration.GetSection("ConnectionStrings:RemoteNew").Value, 
                    contextOptionsBuilder => contextOptionsBuilder.EnableRetryOnFailure());

                options.EnableSensitiveDataLogging();

                options.AddInterceptors(new QueryWithNoLockDbCommandInterceptor());

                options.UseProjectables();

                //options.AddInterceptors(new AuditingInterceptor(configuration.GetSection("ConnectionStrings:RemoteNew").Value));

                //options.UseTriggers(triggerOptions => triggerOptions.AddTrigger<SubjectVisitImageDateTrigger>());

                //options.UseTriggers(triggerOptions => triggerOptions.AddAssemblyTriggers(typeof(SubjectVisitTrigger).Assembly));


                options.UseTriggers(triggerOptions =>
                {
                    triggerOptions.AddTrigger<AddSubjectTrigger>();
                    triggerOptions.AddTrigger<ChallengeStateTrigger>();
                    triggerOptions.AddTrigger<SubjectStateTrigger>();
                    triggerOptions.AddTrigger<SubjectVisitCheckPassedTrigger>();
                    triggerOptions.AddTrigger<SubjectVisitFinalVisitTrigger>();
                    triggerOptions.AddTrigger<SubjectVisitTrigger>();
                    triggerOptions.AddTrigger<TrialCriterionSignTrigger>();
                    triggerOptions.AddTrigger<TableQuestionRowTrigger>();
                    //triggerOptions.AddTrigger<AddlTrialUserTrigger>();
                    triggerOptions.AddTrigger<VisitTaskIsFrontTaskNeedSignButNotSignTrigger>();

                    



                });


            });


            //services.AddAssemblyTriggers(typeof(SubjectVisitImageDateTrigger).Assembly);
        }
    }
}
