using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.Inventories.Units;
using Bp.Sync.Dto.Results;
using Bp.Sync.Dto.SmartTable;
using Microsoft.Extensions.Options;
using Nest;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Bp.Sync.BackEnd.Infrastructure.Repositories
{
    public interface IProductUnitRepository
    {
        Task<BaseResult> Add(ProductUnitElastic Unit);
        Task<BaseResult> AddManyAsync(IEnumerable<ProductUnitElastic> Units);
        Task<SmartTableResult<ProductUnitElastic>> Search(BoolQueryDescriptor<ProductUnitElastic> query);
        Task<SmartTableResult<ProductUnitElastic>> GetByIds(List<long> ids);
        Task<SmartTableResult<ProductUnitElastic>> GetByProductIds(List<long> ids);
        Task<SmartTableResult<ProductUnitElastic>> GetMinUnitByProductIds(List<long> ids);

    }
    public class ProductUnitRepository : IProductUnitRepository
    {
        private readonly ElasticIndex<ProductUnitElastic> _dbContext;

        public ProductUnitRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<ProductUnitElastic>(settings.Value.ClusterUrl, "inventoryproductunit");
        }


        public async Task<SmartTableResult<ProductUnitElastic>> Search(BoolQueryDescriptor<ProductUnitElastic> query)
        {
            return await _dbContext.Search(query);
        }


        public async Task<BaseResult> Add(ProductUnitElastic Unit)
        {
            var result = new BaseResult() { Result = Dto.Results.Result.Success };
            var rs = await _dbContext.Insert(Unit);
            if (rs.IsValid)
            {
                result.Result = Dto.Results.Result.Success;
            }
            else
            {
                result.Result = Dto.Results.Result.SystemError;
                result.Message = "Không tạo được bản ghi";
            }
            return result;
        }
        public async Task<BaseResult> AddManyAsync(IEnumerable<ProductUnitElastic> Units)
        {
            var result = new BaseResult { Result = Dto.Results.Result.Success };
            try
            {
                var rs = await _dbContext.InsertMany(Units);
                if (rs.IsValid) return result;
                else
                {
                    result.Result = Dto.Results.Result.Failed;
                    result.Message = rs.Errors.ToString();
                    return result;
                }
            }
            catch (Exception exx)
            {
                result.Result = Dto.Results.Result.Failed;
                result.Message = exx.Message.ToString();
                return result;
            }
        }

        public async Task<SmartTableResult<ProductUnitElastic>> GetByIds(List<long> ids)
        {
            var query = new BoolQueryDescriptor<ProductUnitElastic>();
            var idsQ = new QueryContainerDescriptor<ProductUnitElastic>();
            idsQ.Terms(t => t.Field(f => f.Id).Terms(ids));
            query.Filter(fl => idsQ);
            return await _dbContext.Search(query);
        }
        public async Task<SmartTableResult<ProductUnitElastic>> GetMinUnitByProductIds(List<long> ids)
        {
            var query = new BoolQueryDescriptor<ProductUnitElastic>();
            var idsQ = new QueryContainerDescriptor<ProductUnitElastic>();
            idsQ.Terms(t => t.Field(f => f.ProductId).Terms(ids));
            var notparentQ = new QueryContainerDescriptor<ProductUnitElastic>();
            notparentQ.Terms(t => t.Field(f => f.ParentUnitId).Terms(0));
            var isdeletedQ = new QueryContainerDescriptor<ProductUnitElastic>();
            isdeletedQ.Terms(t => t.Field(f => f.IsDeleted).Terms(false));
            query.Filter(fl => idsQ && notparentQ && isdeletedQ);
            return await _dbContext.Search(query);
        }

        public async Task<SmartTableResult<ProductUnitElastic>> GetByProductIds(List<long> ids)
        {
            var query = new BoolQueryDescriptor<ProductUnitElastic>();
            var idsQ = new QueryContainerDescriptor<ProductUnitElastic>();
            idsQ.Terms(t => t.Field(f => f.ProductId).Terms(ids));
            var isdeletedQ = new QueryContainerDescriptor<ProductUnitElastic>();
            isdeletedQ.Terms(t => t.Field(f => f.IsDeleted).Terms(false));
            query.Filter(fl => idsQ && isdeletedQ);
            return await _dbContext.Search(query);
        }
    }
}
