using IRaCIS.Application.Interfaces;
using IRaCIS.Application.Contracts;
using IRaCIS.Core.Infra.EFCore;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Application.Filter;
using IRaCIS.Core.Infrastructure.Extention;
using Microsoft.AspNetCore.Mvc;

namespace IRaCIS.Application.Services
{
    [ApiExplorerSettings(GroupName = "Reviewer")]
    public class EducationService : BaseService, IEducationService
    {
        private readonly IRepository<Postgraduate> _postgraduateRepository;
        private readonly IRepository<Education> _educationRepository;

        public EducationService(IRepository<Education> doctorNormalEducationRepository,
            IRepository<Postgraduate> doctorContinueLearningRepository)
        {
            _educationRepository = doctorNormalEducationRepository;
            _postgraduateRepository = doctorContinueLearningRepository;
        }

        /// <summary>
        /// 根据医生Id获取医生教育经历和继续学习经历列表
        /// </summary>
        [HttpGet("{doctorId:Guid}")]
        public async Task<DoctorEducationExperienceDTO> GetEducation(Guid doctorId)
        {
            var educationList = await _educationRepository.Where(o => o.DoctorId == doctorId)
                .OrderBy(t => t.CreateTime).ProjectTo<EducationInfoViewModel>(_mapper.ConfigurationProvider).ToListAsync();

            var postgraduateList = await _postgraduateRepository.Where(o => o.DoctorId == doctorId)
                .OrderBy(t => t.CreateTime).ProjectTo<PostgraduateViewModel>(_mapper.ConfigurationProvider).ToListAsync();


            return new DoctorEducationExperienceDTO()
            {
                EducationList = educationList,
                PostgraduateList = postgraduateList
            };

        }
        /// <summary>
        /// 新增医生教育经历
        /// </summary>
        /// <param name="educationInfoViewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> AddOrUpdateEducationInfo(EducationCommand educationInfoViewModel)
        {
            if (educationInfoViewModel.Id == Guid.Empty || educationInfoViewModel.Id == null)
            {
                var doctorEducationInfo = _mapper.Map<Education>(educationInfoViewModel);
                switch (educationInfoViewModel.Degree)
                {
                    case StaticData.ReviewerDegree.Bachelor:
                        doctorEducationInfo.ShowOrder = 1;
                        break;
                    case StaticData.ReviewerDegree.Master:
                        doctorEducationInfo.ShowOrder = 2;
                        break;
                    case StaticData.ReviewerDegree.Doctorate:
                        doctorEducationInfo.ShowOrder = 3;
                        break;
                }
                await _educationRepository.AddAsync(doctorEducationInfo);
                var success = await _repository.SaveChangesAsync();

                return ResponseOutput.Result(success, doctorEducationInfo.Id.ToString());
            }

            else
            {
                var needUpdate = await _educationRepository.FirstOrDefaultAsync(t => t.Id == educationInfoViewModel.Id);

                if (needUpdate == null) return Null404NotFound(needUpdate);

                _mapper.Map(educationInfoViewModel, needUpdate);

                var success = await _repository.SaveChangesAsync();

                return ResponseOutput.Ok(success);

            }
            //_educationRepository.Update(needUpdate);


        }
       
        [HttpDelete, Route("{doctorId:guid}")]
        public async Task<IResponseOutput> DeleteEducationInfo(Guid id)
        {
            var success = await _educationRepository.BatchDeleteNoTrackingAsync(o => o.Id == id);

            return ResponseOutput.Result(success);
        }
        /// <summary> 添加/更新医生继续学习经历</summary>
       
        [HttpPost]
        public async Task<IResponseOutput> AddOrUpdatePostgraduateInfo(PostgraduateCommand postgraduateViewModel)
        {
            #region 封装前

            //if (postgraduateViewModel.Id == Guid.Empty || postgraduateViewModel.Id == null)
            //{
            //    var doctorContinueLearning = _mapper.Map<Postgraduate>(postgraduateViewModel);
            //    _postgraduateRepository.Add(doctorContinueLearning);
            //    var success = _postgraduateRepository.SaveChanges();

            //    return ResponseOutput.Result(success, doctorContinueLearning.Id.ToString());
            //}
            //else
            //{
            //    _postgraduateRepository.Update(_mapper.Map<Postgraduate>(postgraduateViewModel));
            //    var success = _postgraduateRepository.SaveChanges();

            //    return ResponseOutput.Result(success);

            //}
            #endregion

            var entity =  await _repository.InsertOrUpdateAsync<Postgraduate, PostgraduateCommand>(postgraduateViewModel, true);
            return ResponseOutput.Ok(entity.Id);
        }
        /// <summary>
        /// 删除医生继续学习经历
        /// </summary>
        /// <param name="doctorId">医生Id</param>
        /// <returns></returns>
       
        [HttpDelete("{doctorId:guid}")]
        public async Task<IResponseOutput> DeletePostgraduateInfo(Guid doctorId)
        {
            var success = await _repository.BatchDeleteAsync<Postgraduate>(o => o.Id == doctorId);
            return ResponseOutput.Result(success);
        }
    }
}