using IRaCIS.Application.Contracts;
using IRaCIS.Application.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Panda.DynamicWebApi.Attributes;

namespace IRaCIS.Core.Application.Service
{
    [ApiExplorerSettings(GroupName = "Financial")]
    public class RankPriceService(IRepository<RankPrice> _rankPriceRepository,
        IRepository<ReviewerPayInformation> _reviewerPayInfoRepository, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, IRankPriceService
    {


        [NonDynamicMethod]
        public async Task<IResponseOutput> AddOrUpdateRankPrice(RankPriceCommand addOrUpdateModel, Guid userId)
        {
            if (addOrUpdateModel.Id == Guid.Empty || addOrUpdateModel.Id == null)
            {
                var rankPrice = _mapper.Map<RankPrice>(addOrUpdateModel);
                rankPrice = await _rankPriceRepository.AddAsync(rankPrice);
                if (await _rankPriceRepository.SaveChangesAsync())
                {
                    return ResponseOutput.Ok(rankPrice.Id.ToString());
                }
                else
                {
                    return ResponseOutput.NotOk();
                }

            }
            else
            {
                var success = await _rankPriceRepository.BatchUpdateNoTrackingAsync(t => t.Id == addOrUpdateModel.Id, u => new RankPrice()
                {
                    UpdateUserId = userId,
                    UpdateTime = DateTime.Now,
                    RefresherTraining = addOrUpdateModel.RefresherTraining,
                    RankName = addOrUpdateModel.RankName,
                    Timepoint = addOrUpdateModel.Timepoint,
                    TimepointIn24H = addOrUpdateModel.TimepointIn24H,
                    TimepointIn48H = addOrUpdateModel.TimepointIn48H,
                    Adjudication = addOrUpdateModel.Adjudication,
                    AdjudicationIn24H = addOrUpdateModel.AdjudicationIn24H,
                    AdjudicationIn48H = addOrUpdateModel.AdjudicationIn48H,
                    Global = addOrUpdateModel.Global,
                    Training = addOrUpdateModel.Training,
                    Downtime = addOrUpdateModel.Downtime

                });


                return ResponseOutput.Result(success);
            }
        }


        [HttpDelete("{id:guid}")]
        public async Task<IResponseOutput> DeleteRankPrice(Guid id)
        {

            if (await _reviewerPayInfoRepository.AnyAsync(t => t.RankId == id))
            {
                //---This title has been used by reviewer payment information
                return ResponseOutput.NotOk(_localizer["RP_TitleUsedByRev"]);
            }

            var success = await _rankPriceRepository.BatchDeleteNoTrackingAsync(t => t.Id == id);

            return ResponseOutput.Result(success);
        }

        /// <summary>
        /// 获取职称单价列表
        /// </summary>
        [HttpPost]
        public async Task<PageOutput<RankPriceDTO>> GetRankPriceList(RankPriceQueryDTO inQuery)
        {
            var rankPriceQueryable = _rankPriceRepository.ProjectTo<RankPriceDTO>(_mapper.ConfigurationProvider);
            return await rankPriceQueryable.ToPagedListAsync(inQuery);

        }


        public async Task<List<RankDic>> GetRankDic()
        {
            var rankQueryable = _rankPriceRepository.ProjectTo<RankDic>(_mapper.ConfigurationProvider);
            return await rankQueryable.ToListAsync();

        }

    }
}
