using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.Inventories.Products;
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 IProductInventoryRepository
    {
        Task<BaseResult> Add(ProductInventoryElastic ProductInventory);
        Task<BaseResult> AddManyAsync(IEnumerable<ProductInventoryElastic> ProductInventorys);
        Task<SmartTableResult<ProductInventoryElastic>> Search(BoolQueryDescriptor<ProductInventoryElastic> query);
        Task<SmartTableResult<ProductInventoryElastic>> GetByIds(List<long> ids);
        Task<SmartTableResult<ProductInventoryElastic>> Search(BoolQueryDescriptor<ProductInventoryElastic> query, int count);
        Task<ISearchResponse<ProductInventoryElastic>> SearchAgg(BoolQueryDescriptor<ProductInventoryElastic> query, AggregationContainerDescriptor<ProductInventoryElastic> aggs);

    }
    public class ProductInventoryRepository : IProductInventoryRepository
    {
        private readonly ElasticIndex<ProductInventoryElastic> _dbContext;

        public ProductInventoryRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<ProductInventoryElastic>(settings.Value.ClusterUrl, "inventoryproductinventory");
        }


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


        public async Task<BaseResult> Add(ProductInventoryElastic ProductInventory)
        {
            var result = new BaseResult() { Result = Dto.Results.Result.Success };
            var rs = await _dbContext.Insert(ProductInventory);
            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<ProductInventoryElastic> ProductInventorys)
        {
            var result = new BaseResult { Result = Dto.Results.Result.Success };
            try
            {
                var rs = await _dbContext.InsertMany(ProductInventorys);
                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<ProductInventoryElastic>> GetByIds(List<long> ids)
        {
            var query = new BoolQueryDescriptor<ProductInventoryElastic>();
            var idsQ = new QueryContainerDescriptor<ProductInventoryElastic>();
            idsQ.Terms(t => t.Field(f => f.Id).Terms(ids));
            query.Filter(fl => idsQ);
            return await _dbContext.Search(query);
        }

        public async Task<SmartTableResult<ProductInventoryElastic>> Search(BoolQueryDescriptor<ProductInventoryElastic> query, int count)
        {
            return await _dbContext.Search(query, count);
        }

        public async Task<ISearchResponse<ProductInventoryElastic>> SearchAgg(BoolQueryDescriptor<ProductInventoryElastic> query, AggregationContainerDescriptor<ProductInventoryElastic> aggs)
        {
            return await _dbContext.SearchAgg(query, aggs);
        }

    }
}
