using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.Inventories.Categories;
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 IProductGroupRepository
    {
        Task<BaseResult> Add(ProductGroupElastic ProductGroup);
        Task<BaseResult> AddManyAsync(IEnumerable<ProductGroupElastic> ProductGroups);
        Task<SmartTableResult<ProductGroupElastic>> Search(BoolQueryDescriptor<ProductGroupElastic> query);
        Task<SmartTableResult<ProductGroupElastic>> GetByIds(List<long> ids);
        Task<SmartTableResult<ProductGroupElastic>> GetAll();

    }
    public class ProductGroupRepository : IProductGroupRepository
    {
        private readonly ElasticIndex<ProductGroupElastic> _dbContext;

        public ProductGroupRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<ProductGroupElastic>(settings.Value.ClusterUrl, "inventoryproductgroup");
        }


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


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