using AutoMapper;
using EasyCaching.Core;
using EntityFrameworkCore.Triggered;
using IRaCIS.Core.Application.Service;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure;

namespace IRaCIS.Core.Application.Triggers
{
    /// <summary>
    ///  处理  访视   末次评估  会影响Subject 状态
    /// </summary>
    public class SubjectVisitCheckPassedTrigger : IBeforeSaveTrigger<SubjectVisit>
    {
      
        private readonly IVisitTaskHelpeService _visitTaskHelpeService;


        public SubjectVisitCheckPassedTrigger(
          
            IVisitTaskHelpeService visitTaskHelpeService)
        {
            
        
            _visitTaskHelpeService = visitTaskHelpeService;
        }



        public async Task BeforeSave(ITriggerContext<SubjectVisit> context, CancellationToken cancellationToken)
        {
            var subjectVisit = context.Entity;


            if (context.ChangeType == ChangeType.Modified)
            {

                // 一致性核查通过  生成读片任务
                if (context.UnmodifiedEntity?.CheckState != subjectVisit.CheckState && subjectVisit.CheckState == CheckStateEnum.CVPassed)
                {

                    context.Entity.IsPMBackOrReReading = false;

                    await _visitTaskHelpeService.GenerateVisitTaskAsync(subjectVisit.TrialId, new List<Guid>() { subjectVisit.Id }, true);
                }

            }
        }
    }
}