using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.Inventories;
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 IBatchRepository
    {
        Task<BaseResult> Add(BatchElastic Batch);
        Task<BaseResult> AddManyAsync(IEnumerable<BatchElastic> Batchs);
        Task<BulkResponse> DeleteBatchs(IEnumerable<BatchElastic> Batchs);
        Task<DeleteByQueryResponse> DeleteBatchsByQuery(IEnumerable<long> ids);
        Task<SmartTableResult<BatchElastic>> Search(BoolQueryDescriptor<BatchElastic> query);
        Task<SmartTableResult<BatchElastic>> Search(BoolQueryDescriptor<BatchElastic> query, int count);
        Task<SmartTableResult<BatchElastic>> GetByIds(List<long> ids);

    }
    public class BatchRepository : IBatchRepository
    {
        private readonly ElasticIndex<BatchElastic> _dbContext;

        public BatchRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<BatchElastic>(settings.Value.ClusterUrl, "inventorybatch");
        }

        #region  search

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

        #endregion

        #region Add 

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

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

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

        #endregion
    }
}
