using Bp.Sync.Dto.Results;
using MediatR;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Nest;
using Bp.Sync.Domain.Elastics.FcBusinesses;
using Bp.Sync.BackEnd.Application.Commands.FcBusinesses.InboundStatistics;
using Bp.Sync.BackEnd.Infrastructure.Repositories.FcBusinesses;
using Bp.Sync.BackEnd.Application.Commands.FcBusinesses.OutboundStatistics;
using Bp.Sync.BackEnd.Application.Commands.FcBusinesses.ProductInbounds;

namespace Bp.Sync.BackEnd.Application.Commands.FcBusinesses.Bills
{
    public class UpdateFcChildBillByBillIdHandler : IRequestHandler<UpdateFcChildBillByBillIdCommand, BaseResult>
    {
        private readonly IInboundStatisticRepository _inboundStatisticRepository;
        private readonly IOutboundStatisticRepository _outboundStatisticRepository;
        private readonly IMediator _mediator;

        public UpdateFcChildBillByBillIdHandler(IInboundStatisticRepository inboundStatisticRepository,
            IOutboundStatisticRepository outboundStatisticRepository,
            IMediator mediator)
        {
            _inboundStatisticRepository = inboundStatisticRepository;
            _outboundStatisticRepository = outboundStatisticRepository;
            _mediator = mediator;
        }

        public async Task<BaseResult> Handle(UpdateFcChildBillByBillIdCommand request, CancellationToken cancellationToken)
        {
            var rs = new BaseResult();
            if (!request.Ids.Any()) return new BaseResult { Result = Dto.Results.Result.Failed, Message = "No data" };

            //đồng bộ phiếu nhập
            if (true)
            {
                var query = new BoolQueryDescriptor<InboundStatisticElastic>();

                var idQ = new QueryContainerDescriptor<InboundStatisticElastic>();
                idQ.Terms(t => t.Field(f => f.BillId).Terms(request.Ids));

                query.Filter(fl => idQ);

                var items = await _inboundStatisticRepository.SearchForExcel(query);

                if (items.Items != null || items.Items.Any())
                {
                    var ids = items.Items.Select(x => x.Id).Distinct().ToList();

                    await _mediator.Send(new SyncInboundStatisticCommand(ids));
                    await _mediator.Send(new SyncFcProductInboundCommand(ids));
                }
            }
            //phiếu xuất kho
            if (true)
            {
                var query = new BoolQueryDescriptor<OutboundStatisticElastic>();

                var idQ = new QueryContainerDescriptor<OutboundStatisticElastic>();
                idQ.Terms(t => t.Field(f => f.BillId).Terms(request.Ids));

                query.Filter(fl => idQ);

                var items = await _outboundStatisticRepository.SearchForExcel(query);

                if (items.Items != null || items.Items.Any())
                {
                    var ids = items.Items.Select(x => x.Id).Distinct().ToList();

                    await _mediator.Send(new SyncOutboundStatisticCommand(ids));
                }
            }

            return rs;
        }
    }
}