using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.Systems;
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.Systems
{
    public interface IHistoryElasticRepository
    {
        Task<BaseResult> Add(HistoryElastic History);
        Task<BaseResult> AddManyAsync(IEnumerable<HistoryElastic> Historys);
        Task<BulkResponse> DeleteHistorys(IEnumerable<HistoryElastic> Historys);
        Task<DeleteByQueryResponse> DeleteHistorysByQuery(IEnumerable<long> ids);
        Task<SmartTableResult<HistoryElastic>> Search(BoolQueryDescriptor<HistoryElastic> query);
        Task<SmartTableResult<HistoryElastic>> Search(BoolQueryDescriptor<HistoryElastic> query, int count);
        Task<SmartTableResult<HistoryElastic>> GetByIds(List<long> ids);
        Task<HistoryElastic> GetById(long id);

    }
    public class HistoryElasticRepository : IHistoryElasticRepository
    {
        private readonly ElasticIndex<HistoryElastic> _dbContext;

        public HistoryElasticRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<HistoryElastic>(settings.Value.ClusterUrl, "historyelastic");
        }

        #region  search

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

        #endregion

        #region Add 

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

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

        public async Task<SmartTableResult<HistoryElastic>> GetByIds(List<long> ids)
        {
            var query = new BoolQueryDescriptor<HistoryElastic>();
            var idsQ = new QueryContainerDescriptor<HistoryElastic>();
            idsQ.Terms(t => t.Field(f => f.Id).Terms(ids));
            query.Filter(fl => idsQ);
            return await _dbContext.Search(query);
        }
        public async Task<HistoryElastic> GetById(long id)
        {
            return await _dbContext.GetById(id);
        }

        #endregion
    }
}
