using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.FcBusinesses.Products;
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.FcBusinesses
{
    public interface IFcProductStorageRepository
    {
        Task<BaseResult> Add(FcProductStorageElastic FcProductStorage);
        Task<BaseResult> AddManyAsync(IEnumerable<FcProductStorageElastic> FcProductStorages);
        Task<BulkResponse> DeleteFcProductStorages(IEnumerable<FcProductStorageElastic> FcProductStorages);
        Task<DeleteByQueryResponse> DeleteFcProductStoragesByQuery(IEnumerable<long> ids);
        Task<SmartTableResult<FcProductStorageElastic>> SearchForExcel(BoolQueryDescriptor<FcProductStorageElastic> query);
        Task<SmartTableResult<FcProductStorageElastic>> GetByIds(List<long> ids);
        Task<SmartTableResult<FcProductStorageElastic>> Search(BoolQueryDescriptor<FcProductStorageElastic> query, int count);
        Task<ISearchResponse<FcProductStorageElastic>> SearchAgg(BoolQueryDescriptor<FcProductStorageElastic> query, AggregationContainerDescriptor<FcProductStorageElastic> aggs);

    }
    public class FcProductStorageRepository : IFcProductStorageRepository
    {
        private readonly ElasticIndex<FcProductStorageElastic> _dbContext;

        public FcProductStorageRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<FcProductStorageElastic>(settings.Value.ClusterUrl, "fcproductstorage");
        }

        #region  search

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

        #region Add order

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

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

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

        #endregion
    }
}
