using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.Inventories.Promotions;
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 IPromotionProductRepository
    {
        Task<BaseResult> Add(PromotionProductElastic PromotionProduct);

        Task<BaseResult> AddManyAsync(IEnumerable<PromotionProductElastic> PromotionProducts);

        Task<SmartTableResult<PromotionProductElastic>> Search(BoolQueryDescriptor<PromotionProductElastic> query);

        Task<SmartTableResult<PromotionProductElastic>> GetByIds(List<long> ids);

        Task<SmartTableResult<PromotionProductElastic>> GetAll();
    }

    public class PromotionProductRepository : IPromotionProductRepository
    {
        private readonly ElasticIndex<PromotionProductElastic> _dbContext;

        public PromotionProductRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<PromotionProductElastic>(settings.Value.ClusterUrl, "inventorypromotionproduct");
        }

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

        public async Task<BaseResult> Add(PromotionProductElastic PromotionProduct)
        {
            var result = new BaseResult() { Result = Dto.Results.Result.Success };
            var rs = await _dbContext.Insert(PromotionProduct);
            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<PromotionProductElastic> PromotionProducts)
        {
            var result = new BaseResult { Result = Dto.Results.Result.Success };
            try
            {
                var rs = await _dbContext.InsertMany(PromotionProducts);
                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<SmartTableResult<PromotionProductElastic>> GetByIds(List<long> ids)
        {
            var query = new BoolQueryDescriptor<PromotionProductElastic>();
            var idsQ = new QueryContainerDescriptor<PromotionProductElastic>();
            idsQ.Terms(t => t.Field(f => f.Id).Terms(ids));
            query.Filter(fl => idsQ);
            return await _dbContext.Search(query);
        }

        public async Task<SmartTableResult<PromotionProductElastic>> GetAll()
        {
            var query = new BoolQueryDescriptor<PromotionProductElastic>();
            var isdeletedQ = new QueryContainerDescriptor<PromotionProductElastic>();
            isdeletedQ.Terms(t => t.Field(f => f.IsDeleted).Terms(false));
            query.Filter(f => isdeletedQ);
            return await _dbContext.Search(query);
        }
    }
}