using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.FcBusinesses;
using Bp.Sync.Domain.Elastics.FcCustomers.Customers;
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.FcCustomers
{
    public interface IFcCustomerRepository
    {
        Task<BaseResult> Add(FcCustomerElastic FcCustomer);
        Task<BaseResult> AddManyAsync(IEnumerable<FcCustomerElastic> FcCustomers);
        Task<BulkResponse> DeleteFcCustomers(IEnumerable<FcCustomerElastic> FcCustomers);
        Task<DeleteByQueryResponse> DeleteFcCustomersByQuery(IEnumerable<long> ids);
        Task<SmartTableResult<FcCustomerElastic>> SearchForExcel(BoolQueryDescriptor<FcCustomerElastic> query);
        Task<SmartTableResult<FcCustomerElastic>> GetByIds(List<long> ids);

    }
    public class FcCustomerRepository : IFcCustomerRepository
    {
        private readonly ElasticIndex<FcCustomerElastic> _dbContext;

        public FcCustomerRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<FcCustomerElastic>(settings.Value.ClusterUrl, "fccustomer");
        }

        #region  search

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

        #endregion

        #region Add order

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

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

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

        #endregion
    }
}
