using IRaCIS.Application.Interfaces;
using IRaCIS.Application.Contracts;
using IRaCIS.Core.Application.Filter;
using IRaCIS.Core.Infra.EFCore;
using IRaCIS.Core.Domain.Models;
using IRaCIS.Core.Infrastructure.Extention;
using Microsoft.AspNetCore.Mvc;
using Panda.DynamicWebApi.Attributes;

namespace IRaCIS.Application.Services
{

    [ApiExplorerSettings(GroupName = "Financial")]
    public class ExchangeRateService : BaseService, IExchangeRateService
    {
        private readonly IRepository<ExchangeRate> _exchangeRateRepository;
        private readonly IRepository<Payment> _paymentRepository;

        public ExchangeRateService(IRepository<ExchangeRate> exchangeRateRepository, IRepository<Payment> paymentRepository)
        {
            _exchangeRateRepository = exchangeRateRepository;
            _paymentRepository = paymentRepository;
        }

        [NonDynamicMethod]
        public async Task<IResponseOutput> AddOrUpdateExchangeRate(ExchangeRateCommand model)
        {
            if (model.Id == Guid.Empty || model.Id == null)
            {
                var existItem = await _exchangeRateRepository.FirstOrDefaultAsync(u => u.YearMonth == model.YearMonth);
                if (existItem != null)
                {
                    return ResponseOutput.NotOk("The exchange rate of the same month already existed.");
                }
                var rate = _mapper.Map<ExchangeRate>(model);
                rate = await _exchangeRateRepository.AddAsync(rate);
                if (await _exchangeRateRepository.SaveChangesAsync())
                {
                    return ResponseOutput.Ok(rate.Id.ToString());
                }
                else
                {
                    return ResponseOutput.NotOk();
                }

            }
            else
            {
                var success = await _exchangeRateRepository.BatchUpdateNoTrackingAsync(t => t.Id == model.Id, u => new ExchangeRate()
                {
                    //YearMonth = model.YearMonth,
                    Rate = model.Rate,
                    UpdateTime = DateTime.Now
                });
                return ResponseOutput.Result(success);
            }
        }

        /// <summary>
        /// 根据记录Id，删除汇率记录
        /// </summary>
        /// <param name="id">汇率记录Id</param>
       
        [HttpDelete("{id:guid}")]
        public async Task<IResponseOutput> DeleteExchangeRate(Guid id)
        {
            var monthInfo = await _exchangeRateRepository.FirstOrDefaultAsync(t => t.Id == id);

            if (await _paymentRepository.AnyAsync(t => t.YearMonth == monthInfo.YearMonth))
            {
                return ResponseOutput.NotOk("The exchange rate has been used in monthly payment");
            }


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

            return ResponseOutput.Ok(success); 
        }


        [NonDynamicMethod]
        public async Task<decimal> GetExchangeRateByMonth(string month)
        {
            //var rate = _exchangeRateRepository.FindSingleOrDefault(u => u.YearMonth.Equals(month));
            //if (rate == null)
            //{
            //    return 0;
            //}
            //return rate.Rate;

            var rate = await _exchangeRateRepository.FirstOrDefaultAsync(t => t.YearMonth == month);
            if (rate == null)
            {
                return 0;
            }
            return rate.Rate;
        }

        [HttpPost]
        public async Task<PageOutput<ExchangeRateCommand>> GetExchangeRateList(ExchangeRateQueryDTO queryParam)
        {

            var yearMonth = queryParam.SearchMonth?.ToString("yyyy-MM");
            
            var exchangeRateQueryable = _exchangeRateRepository.AsQueryable()
                .WhereIf(queryParam.SearchMonth != null, o => o.YearMonth == yearMonth)
                .ProjectTo<ExchangeRateCommand>(_mapper.ConfigurationProvider);

            return await exchangeRateQueryable.ToPagedListAsync(queryParam.PageIndex, queryParam.PageSize, "YearMonth", false);



        }
    }
}
