using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.FcBusinesses.Bills;
using Bp.Sync.Domain.Elastics.FcBusinesses.UnitExchanges;
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 IFcUnitExchangeRepository
    {
        Task<BaseResult> Add(FcUnitExchangeElastic FcUnitExchange);
        Task<BaseResult> AddManyAsync(IEnumerable<FcUnitExchangeElastic> FcUnitExchanges);
        Task<BulkResponse> DeleteFcUnitExchanges(IEnumerable<FcUnitExchangeElastic> FcUnitExchanges);
        Task<DeleteByQueryResponse> DeleteFcUnitExchangesByQuery(IEnumerable<long> ids);
        Task<SmartTableResult<FcUnitExchangeElastic>> SearchForExcel(BoolQueryDescriptor<FcUnitExchangeElastic> query);
        Task<SmartTableResult<FcUnitExchangeElastic>> GetByProductIds(List<long> ids);

    }
    public class FcUnitExchangeRepository : IFcUnitExchangeRepository
    {
        private readonly ElasticIndex<FcUnitExchangeElastic> _dbContext;

        public FcUnitExchangeRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<FcUnitExchangeElastic>(settings.Value.ClusterUrl, "fcunitexchange");
        }

        #region  search

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

        #endregion

        #region Add order

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

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

        public async Task<SmartTableResult<FcUnitExchangeElastic>> GetByProductIds(List<long> ids)
        {
            var query = new BoolQueryDescriptor<FcUnitExchangeElastic>();
            var notDeletedQ = new QueryContainerDescriptor<FcUnitExchangeElastic>();
            notDeletedQ.Terms(t => t.Field(f => f.IsDeleted).Terms(false));
            var idsQ = new QueryContainerDescriptor<FcUnitExchangeElastic>();
            idsQ.Terms(t => t.Field(f => f.ProductId).Terms(ids));
            query.Filter(fl => idsQ && notDeletedQ);
            return await _dbContext.Search(query);
        }

        #endregion
    }
}
