using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
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 IInventoryBillRepository
    {
        Task<BaseResult> Add(InventoryBillElastic InventoryBill);
        Task<BaseResult> AddManyAsync(IEnumerable<InventoryBillElastic> InventoryBills);
        Task<BulkResponse> DeleteInventoryBills(IEnumerable<InventoryBillElastic> InventoryBills);
        Task<DeleteByQueryResponse> DeleteInventoryBillsByQuery(IEnumerable<long> ids);
        Task<SmartTableResult<BaseLong>> SearchIds(BoolQueryDescriptor<InventoryBillElastic> query);
        Task<SmartTableResult<InventoryBillElastic>> SearchForExcel(BoolQueryDescriptor<InventoryBillElastic> query);
        Task<SmartTableResult<InventoryBillElastic>> Search(BoolQueryDescriptor<InventoryBillElastic> query, int count);

    }
    public class InventoryBillRepository : IInventoryBillRepository
    {
        private readonly ElasticIndex<InventoryBillElastic> _dbContext;

        public InventoryBillRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<InventoryBillElastic>(settings.Value.ClusterUrl, "inventoryInventorybill");
        }

        #region  search

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

        #endregion

        #region Add

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

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

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

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

        #endregion
    }
}
