using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.Inventories;
using Bp.Sync.Domain.Elastics.Inventories.Bills;
using Bp.Sync.Dto.Models;
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 IOutboundBillRepository
    {
        Task<BaseResult> Add(OutboundBillElastic OutboundBill);
        Task<BaseResult> AddManyAsync(IEnumerable<OutboundBillElastic> OutboundBills);
        Task<BulkResponse> DeleteOutboundBills(IEnumerable<OutboundBillElastic> OutboundBills);
        Task<DeleteByQueryResponse> DeleteOutboundBillsByQuery(IEnumerable<long> ids);
        Task<SmartTableResult<BaseLong>> SearchIds(BoolQueryDescriptor<OutboundBillElastic> query);
        Task<SmartTableResult<OutboundBillElastic>> SearchForExcel(BoolQueryDescriptor<OutboundBillElastic> query);
        Task<SmartTableResult<OutboundBillElastic>> Search(BoolQueryDescriptor<OutboundBillElastic> query, int count);
        Task<ISearchResponse<OutboundBillElastic>> SearchAgg(BoolQueryDescriptor<OutboundBillElastic> query, AggregationContainerDescriptor<OutboundBillElastic> aggs);

    }
    public class OutboundBillRepository : IOutboundBillRepository
    {
        private readonly ElasticIndex<OutboundBillElastic> _dbContext;

        public OutboundBillRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<OutboundBillElastic>(settings.Value.ClusterUrl, "inventoryoutboundbill");
        }

        #region  search

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

        #endregion

        #region Add order

        public async Task<BaseResult> Add(OutboundBillElastic OutboundBill)
        {
            var result = new BaseResult() { Result = Dto.Results.Result.Success };
            var rs = await _dbContext.Insert(OutboundBill);
            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<OutboundBillElastic> OutboundBills)
        {
            var result = new BaseResult { Result = Dto.Results.Result.Success };
            try
            {
                var rs = await _dbContext.InsertMany(OutboundBills);
                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<BulkResponse> DeleteOutboundBills(IEnumerable<OutboundBillElastic> OutboundBills)
        {
            return await _dbContext.DeleteMany(OutboundBills);
        }

        public async Task<DeleteByQueryResponse> DeleteOutboundBillsByQuery(IEnumerable<long> ids)
        {
            return await _dbContext.DeleteByIds(ids);
        }

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

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

        public async Task<SmartTableResult<BaseLong>> SearchIds(BoolQueryDescriptor<OutboundBillElastic> query)
        {
            return await _dbContext.SearchIds(query);
        }

        #endregion
    }
}
