using IRaCIS.Application.Interfaces;
using IRaCIS.Core.Application.Helper;
using IRaCIS.Core.Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using System.Text;

namespace IRaCIS.Core.Application.Service
{
    [ApiExplorerSettings(GroupName = "Common")]
    public class FileService : BaseService
    {




        /// <summary>
        /// New  医生首页 多选 获取多个医生信息+文件路径列表  医生压缩包名称  doctorCode + "_" + doctorName _(时间戳或者随机的Guid)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput<List<DoctorDownloadInfo>>> GetOfficialResume(GetDoctorPathCommand command,
            [FromServices] IRepository<Attachment> _attachmentrepository)
        {

            var list = await _attachmentrepository.Where(t => command.DoctorIdList.Contains(t.DoctorId) && command.Language == t.Language).GroupBy(t => new { Name = t.Doctor.FirstName + "_" + t.Doctor.LastName, ReviewerCode = t.Doctor.ReviewerCode, t.DoctorId })
                .Select(g => new DoctorDownloadInfo()
                {
                    Id = g.Key.DoctorId,
                    Name = g.Key.Name,
                    ReviewerCode = g.Key.ReviewerCode,
                    FileList = g.Select(t => new DownloadFileInfo() { FileName = t.FileName, Path = t.Path }).ToList()
                }).ToListAsync();

            return ResponseOutput.Ok(list);

        }

        /// <summary>
        /// New  项目入组  勾选获取简历路径
        /// </summary>
        /// <param name="command"></param>
        /// <param name="_doctorService"></param>
        /// <param name="_attachmentrepository"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput<List<DoctorDownloadInfo>>> GetTrialDoctorOfficialResume(GetDoctorPathCommand command,
           [FromServices] IDoctorService _doctorService,
           [FromServices] IRepository<Attachment> _attachmentrepository)
        {

            var list = await _attachmentrepository.Where(t => command.DoctorIdList.Contains(t.DoctorId) && command.Language == t.Language && t.IsOfficial && t.Type.Equals("Resume")).GroupBy(t => new { Name = t.Doctor.FirstName + "_" + t.Doctor.LastName, ReviewerCode = t.Doctor.ReviewerCode, t.DoctorId })
                .Select(g => new DoctorDownloadInfo()
                {
                    Id = g.Key.DoctorId,
                    Name = g.Key.Name,
                    ReviewerCode = g.Key.ReviewerCode,
                    FileList = g.Select(t => new DownloadFileInfo() { FileName = t.FileName, Path = t.Path }).ToList()
                }).ToListAsync();

            return ResponseOutput.Ok(list);

        }

        /// <summary>
        /// new  医生详情  勾选或者下载文件路径
        /// </summary>
        /// <param name="command"></param>
        /// <param name="_doctorService"></param>
        /// <param name="_attachmentrepository"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput<DoctorDownloadInfo>> GetDoctorAttachment(GetDoctoreAttachPathCommand command,
         [FromServices] IDoctorService _doctorService,
         [FromServices] IRepository<Attachment> _attachmentrepository)
        {

            var find = await _attachmentrepository.Where(t => command.DoctorId == t.DoctorId && command.AttachmentIdList.Contains(t.Id)).GroupBy(t => new { Name = t.Doctor.FirstName + "_" + t.Doctor.LastName, ReviewerCode = t.Doctor.ReviewerCode, t.DoctorId })
                .Select(g => new DoctorDownloadInfo()
                {
                    Id = g.Key.DoctorId,
                    Name = g.Key.Name,
                    ReviewerCode = g.Key.ReviewerCode,
                    FileList = g.Select(t => new DownloadFileInfo() { FileName = t.FileName, Path = t.Path }).ToList()
                }).FirstOrDefaultAsync();

            return ResponseOutput.Ok(find);

        }

    }

    #region DTO
    public class DoctorDownloadInfo
    {
        public Guid? Id { get; set; }
        public string Name { get; set; }
        public string ReviewerCode { get; set; }

        public List<DownloadFileInfo> FileList { get; set; }
    }
    public class DownloadFileInfo
    {
        public string FileName { get; set; }

        public string Path { get; set; }
    }

    public class GetDoctorPathCommand
    {
        public int Language { get; set; }

        public List<Guid?> DoctorIdList { get; set; }
    }

    public class GetDoctoreAttachPathCommand
    {
        public Guid DoctorId { get; set; }
        public List<Guid> AttachmentIdList { get; set; }
    }
    #endregion
}
