using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.Finances;
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 ITransactionRepository
    {
        Task<BaseResult> Add(TransactionElastic order);
        Task<BaseResult> AddManyAsync(IEnumerable<TransactionElastic> orders);
        Task<BulkResponse> DeleteTransactions(IEnumerable<TransactionElastic> timeMonitors);
        Task<DeleteByQueryResponse> DeleteTransactionsByQuery(IEnumerable<long> ids);
        Task<SmartTableResult<TransactionElastic>> SearchForExcel(BoolQueryDescriptor<TransactionElastic> query);

    }
    public class TransactionRepository : ITransactionRepository
    {
        private readonly ElasticIndex<TransactionElastic> _dbContext;

        public TransactionRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<TransactionElastic>(settings.Value.ClusterUrl, "financetransaction");
        }

        #region  search

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

        #endregion

        #region Add order

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

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

        #endregion
    }
}
