using Bp.Sync.BackEnd.Infrastructure.Repositories;
using Bp.Sync.BackEnd.Infrastructure.Repositories.FcBusinesses;
using Bp.Sync.Domain.Elastics.Inventories;
using Bp.Sync.Domain.Elastics.Inventories.Bills;
using Bp.Sync.Domain.Elastics.Inventories.Configurations;
using Bp.Sync.Domain.Elastics.Inventories.Products;
using Bp.Sync.Domain.Entities.Inventories.Products;
using Bp.Sync.Dto.Results;
using Bp.Sync.Dto.SmartTable;
using CommonLibrary;
using Dapper;
using EasyNetQ;
using MediatR;
using Microsoft.Extensions.Configuration;
using Nest;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Vina.Search.Infrastructure.Mappings;

namespace Bp.Sync.BackEnd.Application.Commands.Inventories.Products
{
    public class UpdateExpiryDateProductInventoryHandler : IRequestHandler<UpdateExpiryDateProductInventoryCommand, BaseResult>
    {
        private readonly IDbConnection _dbConnection;
        private readonly IProductInventoryRepository _ProductInventoryRepository;
        private readonly IBatchRepository _batchRepository;
        private readonly IBus _bus;
        private readonly IProductBillRepository _producBillRepository;
        private readonly IContractDiscountLevelRepository _contractDiscountLevelRepository;
        private readonly IInboundBillRepository _inboundBillRepository;

        public UpdateExpiryDateProductInventoryHandler(IConfiguration configuration, IProductInventoryRepository ProductInventoryRepository,
            IInboundBillRepository inboundBillRepository,
            IContractDiscountLevelRepository contractDiscountLevelRepository,
            IBus bus, IBatchRepository batchRepository,
            IProductBillRepository producBillRepository)
        {
            var connectionString = configuration.GetConnectionString("InventoryConnection");
            _dbConnection = new SqlConnection(connectionString);
            _ProductInventoryRepository = ProductInventoryRepository;
            _bus = bus;
            _batchRepository = batchRepository;
            _producBillRepository = producBillRepository;
            _contractDiscountLevelRepository = contractDiscountLevelRepository;
            _inboundBillRepository = inboundBillRepository;
        }

        public async Task<BaseResult> Handle(UpdateExpiryDateProductInventoryCommand request, CancellationToken cancellationToken)
        {
            var rs = new BaseResult();

            if (request.Ids.Count() > 1000)
            {
                var length = request.Ids.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / 1000; i++)
                    {
                        if (length > i * 1000)
                        {
                            var ids1 = request.Ids.Skip(i * 1000).Take(1000).ToList();
                            if (ids1.Count() > 0)
                            {
                                await _bus.SendAsync($"UpdateToElasticProductInventoryNotEx", ids1.ConvertListLongToString());
                            }
                        }
                    }
                }
                return new BaseResult { Result = Dto.Results.Result.Success, Message = $"sent {length} update ProductInventorys" };
            }

            var ids = request.Ids;
            var listUpdates = new List<long>();

            var batches = new SmartTableResult<BatchElastic> { Items = new List<BatchElastic>() };
            var contractProductids = new List<long>();
            if (true)
            {
                var queryPI = new BoolQueryDescriptor<ContractDiscountLevelElastic>();
                var aggsPI = new AggregationContainerDescriptor<ContractDiscountLevelElastic>();
                var isDeletedPIQ = new QueryContainerDescriptor<ContractDiscountLevelElastic>();
                isDeletedPIQ.Terms(t => t.Field(f => f.IsDeleted).Terms(false));
                aggsPI.Terms("product", t => t.Field(f => f.ProductId).Size(10000));
                queryPI.Filter(f => isDeletedPIQ);
                var rsPI = await _contractDiscountLevelRepository.SearchAgg(queryPI, aggsPI);
                var aggPI = rsPI.Aggregations.Terms("product");
                if (aggPI != null)
                {
                    contractProductids = aggPI.Buckets.Select(x => long.Parse(x.Key)).ToList();
                }
            }
            var contractProductQ = new QueryContainerDescriptor<ProductInventoryElastic>();
            contractProductQ.Terms(e => e.Field(f => f.ProductId).Terms(contractProductids));
            var notInventoryCodeBillCreatedTimeQ = new QueryContainerDescriptor<ProductInventoryElastic>();
            notInventoryCodeBillCreatedTimeQ.Bool(b => b.MustNot(m => m.Exists(e => e.Field(f => f.InventoryCodeInboundCreatedTime))));

            if (!ids.Any())
            {
                //lay ra danh sach don k co expiry date
                var query = new BoolQueryDescriptor<ProductInventoryElastic>();
                var notExpiryQ = new QueryContainerDescriptor<ProductInventoryElastic>();
                notExpiryQ.Bool(b => b.MustNot(m => m.Exists(e => e.Field(f => f.ExpiryDate))));
                var quantity0Q = new QueryContainerDescriptor<ProductInventoryElastic>();
                quantity0Q.Range(e => e.Field(f => f.Quantity).GreaterThan(0));
                var notDelete1Q = new QueryContainerDescriptor<ProductInventoryElastic>();
                notDelete1Q.Terms(t => t.Field(f => f.IsDeleted).Terms(false));

                var idQ = new QueryContainerDescriptor<ProductInvoiceElastic>();
                if (ids.Any()) idQ.Terms(t => t.Field(f => f.Id).Terms(request.Ids));
                if (ids.Any())
                    query.Filter(fl => idQ);
                else if (contractProductids.Any())
                    query.Filter(f => notDelete1Q && (notExpiryQ || (contractProductQ && notInventoryCodeBillCreatedTimeQ)));
                else
                    query.Filter(f => notDelete1Q && notExpiryQ);
                var ins = await _ProductInventoryRepository.Search(query, 10000);

                var needs = ins.Items.OrderBy(x => x.Id).ToList();

                var step = 1000;
                var lenth = needs.Count / step;
                for (int i = 0; i <= lenth; i++)
                {
                    var productIds = new List<long>();
                    var codes = new List<string>();

                    var steps = needs.Skip(i * step).Take(step).ToList();
                    if (steps.Any())
                        productIds = steps.Select(x => x.ProductId).Distinct().ToList();
                    if (steps.Any())
                        codes = steps.Select(x => x.InventoryCode).Distinct().ToList();
                    //lấy thông tin ngày hết hạn
                    var queryBatch = new BoolQueryDescriptor<BatchElastic>();
                    var inventoryCodeQ = new QueryContainerDescriptor<BatchElastic>();
                    if (codes.Any())
                        inventoryCodeQ.Terms(e => e.Field(f => f.InventoryCode.Suffix("keyword")).Terms(codes));

                    var productInQ = new QueryContainerDescriptor<BatchElastic>();
                    productInQ.Terms(e => e.Field(f => f.ProductId).Terms(ids));
                    var notDeleteQ = new QueryContainerDescriptor<BatchElastic>();
                    notDeleteQ.Terms(t => t.Field(f => f.IsDeleted).Terms(false));

                    queryBatch.Filter(f => inventoryCodeQ && productInQ && notDeleteQ);
                    var batches2 = await _batchRepository.Search(queryBatch, 10000);
                    if (batches2.Items.Any()) batches.Items = batches.Items.Concat(batches2.Items);
                }

                if (!batches.Items.Any())
                {
                    rs.Message = $"no data to update";
                    rs.Result = Dto.Results.Result.Success;
                    return rs;
                }

                var prIds = batches.Items.Select(x => x.ProductId).Distinct().ToList();

                if (!prIds.Any())
                {
                    rs.Message = $"no data to update";
                    rs.Result = Dto.Results.Result.Success;
                    return rs;
                }

                ins.Items = ins.Items.Where(x => prIds.Contains(x.ProductId)).ToList();

                foreach (var item in ins.Items)
                {
                    var br = batches.Items.FirstOrDefault(x => x.InventoryCode == item.InventoryCode && x.ProductId == item.ProductId);
                    if (br != null) listUpdates.Add(item.Id);
                }
            }

            if (!listUpdates.Any())
            {
                rs.Message = $"no data to update";
                rs.Result = Dto.Results.Result.Success;
                return rs;
            }

            try
            {
                var p = new Dto.DapperTypes.IdsParam(listUpdates);
                var ProductInventorys = await _dbConnection.QueryAsync<ProductInventory>("Sync_ProductInventory_Get_By_List_Id", p, commandType: CommandType.StoredProcedure);
                if (ProductInventorys.Any())
                {
                    var items = ProductInventorys.ToProductInventorys();

                    var productIds = items.Select(x => x.ProductId).Distinct().ToList();
                    var inventoryCodes = items.Where(x => !string.IsNullOrEmpty(x.InventoryCode) && !string.IsNullOrWhiteSpace(x.InventoryCode)).Select(x => x.InventoryCode).Distinct().ToList();

                    //lay ra productbill
                    var queryProductBill = new BoolQueryDescriptor<ProductBillElastic>();
                    var notDeleteProductBillQ = new QueryContainerDescriptor<ProductBillElastic>();
                    notDeleteProductBillQ.Terms(t => t.Field(f => f.IsDeleted).Terms(false));
                    var productProductBillQ = new QueryContainerDescriptor<ProductBillElastic>();
                    productProductBillQ.Terms(e => e.Field(f => f.ProductId).Terms(productIds));
                    var booktypeQ = new QueryContainerDescriptor<ProductBillElastic>();
                    booktypeQ.Terms(e => e.Field(f => f.BookType).Terms(2));
                    var priceQ = new QueryContainerDescriptor<ProductBillElastic>();
                    priceQ.Range(e => e.Field(f => f.Price).GreaterThan(0));
                    queryProductBill.Filter(f => notDeleteProductBillQ && productProductBillQ && booktypeQ && priceQ);
                    var aggsProductBill = new AggregationContainerDescriptor<ProductBillElastic>();
                    aggsProductBill.Terms("product", s2 => s2.Field(f2 => f2.ProductId).Size(10000).Aggregations(a2 => a2.Max("id", s2 => s2.Field(f2 => f2.VocherDateToLong))));
                    var rsprBIlls = await _producBillRepository.SearchAgg(queryProductBill, aggsProductBill);

                    var listvocher = new List<long>();
                    if (rsprBIlls != null)
                    {
                        var r = rsprBIlls.Aggregations.Terms("product");
                        foreach (var item in r.Buckets)
                        {
                            var productInven = items.Where(x => x.ProductId == long.Parse(item.Key)).ToList();
                            var r2 = item.Max("id");
                            if (r2.Value.HasValue)
                            {
                                foreach (var item2 in productInven)
                                {
                                    item2.ProductBillVocherDateToLong = (long)r2.Value.Value;
                                }
                                listvocher.Add((long)r2.Value.Value);
                            }
                        }
                    }

                    var listproducBills = new List<ProductBillElastic>();
                    if (listvocher.Any())
                    {
                        var vocherQ = new QueryContainerDescriptor<ProductBillElastic>();
                        vocherQ.Terms(e => e.Field(f => f.VocherDateToLong).Terms(listvocher));
                        queryProductBill.Filter(f => notDeleteProductBillQ && vocherQ && productProductBillQ);

                        var r3 = await _producBillRepository.Search(queryProductBill, 10000);
                        if (r3.Items.Any()) listproducBills = r3.Items.ToList();
                    }

                    var inbounds = new List<InboundBillElastic>();
                    if (contractProductids.Any())
                    {
                        var queryPI = new BoolQueryDescriptor<InboundBillElastic>();
                        var isDeletedPIQ = new QueryContainerDescriptor<InboundBillElastic>();
                        isDeletedPIQ.Terms(t => t.Field(f => f.IsDeleted).Terms(false));
                        var productIQ = new QueryContainerDescriptor<InboundBillElastic>();
                        productIQ.Terms(t => t.Field(f => f.ProductId).Terms(contractProductids));
                        var billStatusIQ = new QueryContainerDescriptor<InboundBillElastic>();
                        billStatusIQ.Terms(t => t.Field(f => f.BillStatus).Terms(3));
                        var inboundStatusIQ = new QueryContainerDescriptor<InboundBillElastic>();
                        inboundStatusIQ.Terms(t => t.Field(f => f.InboundStatus).Terms(3));
                        var billTypeIQ = new QueryContainerDescriptor<InboundBillElastic>();
                        billTypeIQ.Terms(t => t.Field(f => f.BillType).Terms(1));

                        var codes = items.Where(x => contractProductids.Contains(x.ProductId)).Select(x => x.InventoryCode).Distinct().ToList();
                        var inventoryCodeIQ = new QueryContainerDescriptor<InboundBillElastic>();
                        if (codes.Any())
                            inventoryCodeIQ.Terms(e => e.Field(f => f.InventoryCode.Suffix("keyword")).Terms(codes));
                        if (codes.Any())
                        {
                            queryPI.Filter(f => isDeletedPIQ && productIQ && billStatusIQ && inboundStatusIQ && inventoryCodeIQ && billTypeIQ);
                            var rsPI = await _inboundBillRepository.Search(queryPI, 10000);
                            inbounds = rsPI.Items.ToList();
                        }
                    }

                    int updated = 0;
                    foreach (var item in items)
                    {
                        if (batches.Items.Any())
                        {
                            var br = batches.Items.FirstOrDefault(x => x.InventoryCode == item.InventoryCode && x.ProductId == item.ProductId);
                            if (br != null)
                                item.ExpiryDate = br.ExpiryDate;
                        }

                        if (listproducBills.Any())
                        {
                            var br = listproducBills.FirstOrDefault(x => x.ProductId == item.ProductId && x.VocherDateToLong == item.ProductBillVocherDateToLong);
                            if (br != null)
                            {
                                item.ProductBillPrice = /*br.InventoryQuantity > 0 ? br.InventoryPrice / br.InventoryQuantity*/ br.Price * item.Quantity/* : 0*/;
                            }
                        }
                        var inbound = inbounds.FirstOrDefault(x => x.InventoryCode == item.InventoryCode);
                        if (inbound != null)
                        {
                            item.InventoryCodeInboundCreatedTime = inbound.CreatedTime;
                            item.InventoryCodeBillId = inbound.BillId;
                        }
                        updated++;
                    }

                    var rs2 = await _ProductInventoryRepository.AddManyAsync(items);
                    if (rs2.Result == Dto.Results.Result.Success)
                    {
                        rs.Message = $"{updated} update";
                        rs.Result = Dto.Results.Result.Success;
                    }
                    else
                    {
                        rs.Message = $"can not insert db";
                        rs.Result = Dto.Results.Result.Success;
                    }
                }
                else
                {
                    rs.Message = $"no data to update";
                    rs.Result = Dto.Results.Result.Success;
                }
            }
            catch (Exception ex)
            {
                rs.Message = ex.Message.ToString();
                rs.Result = Dto.Results.Result.SystemError;
            }
            return rs;
        }
    }
}