using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.Inventories.Configurations;
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 IContractDiscountLevelRepository
    {
        Task<BaseResult> Add(ContractDiscountLevelElastic ContractDiscountLevel);

        Task<BaseResult> AddManyAsync(IEnumerable<ContractDiscountLevelElastic> ContractDiscountLevels);

        Task<SmartTableResult<ContractDiscountLevelElastic>> Search(BoolQueryDescriptor<ContractDiscountLevelElastic> query);

        Task<ISearchResponse<ContractDiscountLevelElastic>> SearchAgg(BoolQueryDescriptor<ContractDiscountLevelElastic> query, AggregationContainerDescriptor<ContractDiscountLevelElastic> aggs);

        Task<SmartTableResult<ContractDiscountLevelElastic>> Search(BoolQueryDescriptor<ContractDiscountLevelElastic> query, int count);
    }

    public class ContractDiscountLevelRepository : IContractDiscountLevelRepository
    {
        private readonly ElasticIndex<ContractDiscountLevelElastic> _dbContext;

        public ContractDiscountLevelRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<ContractDiscountLevelElastic>(settings.Value.ClusterUrl, "inventorycontractdiscountlevel");
        }

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

        public async Task<BaseResult> Add(ContractDiscountLevelElastic ContractDiscountLevel)
        {
            var result = new BaseResult() { Result = Dto.Results.Result.Success };
            var rs = await _dbContext.Insert(ContractDiscountLevel);
            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<ContractDiscountLevelElastic> ContractDiscountLevels)
        {
            var result = new BaseResult { Result = Dto.Results.Result.Success };
            try
            {
                var rs = await _dbContext.InsertMany(ContractDiscountLevels);
                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<ISearchResponse<ContractDiscountLevelElastic>> SearchAgg(BoolQueryDescriptor<ContractDiscountLevelElastic> query, AggregationContainerDescriptor<ContractDiscountLevelElastic> aggs)
        {
            return await _dbContext.SearchAgg(query, aggs);
        }

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