using Hotline.Orders;
using Hotline.Quality;
using MapsterMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hotline.Repository.SqlSugar.DataPermissions;
using SqlSugar;
using XF.Domain.Authentications;
using XF.Domain.Dependency;
using Hotline.Share.Dtos.Quality;
using Hotline.Share.Enums.Quality;
using Hotline.Share.Dtos.Order;
using XF.Domain.Repository;

namespace Hotline.Repository.SqlSugar.Orders
{
    public class CitizenRepository : BaseRepository<Citizen>, ICitizenRepository, IScopeDependency
	{
		private readonly ISessionContext _sessionContext;
		private readonly IMapper _mapper;
		private readonly IOrderRepository _orderRepository;
		private readonly IRepository<CitizenLabelDetail> _labelDetailRepository;

		public CitizenRepository(
			ISugarUnitOfWork<HotlineDbContext> uow,
			IDataPermissionFilterBuilder dataPermissionFilterBuilder,
			ISessionContext sessionContext,
			IOrderRepository orderRepository,
			IRepository<CitizenLabelDetail> labelDetailRepository,
			IMapper mapper, IServiceProvider serviceProvider
        ) : base(uow, dataPermissionFilterBuilder, serviceProvider)
		{
			_sessionContext = sessionContext;
			_orderRepository = orderRepository;
			_labelDetailRepository = labelDetailRepository;
			_mapper = mapper;
		}

		public async Task AddAsync(CitizenAddDto model, CancellationToken cancellationToken)
		{
			var firstCallTime = await _orderRepository.Queryable().Where(x => x.Contact == model.PhoneNumber).MinAsync(x => x.CreationTime);
			firstCallTime = firstCallTime == null ? DateTime.Now : firstCallTime;
			model.FirstCallTime = firstCallTime;
			var citizen = _mapper.Map<Citizen>(model);
			await AddAsync(citizen, cancellationToken);
		}

        public async Task<Citizen> GetByPhoneNumberAsync(string phoneNumber)
            => await Queryable().Where(m => m.PhoneNumber == phoneNumber).FirstAsync();

        public async Task LabelAddAsync(LabelDetailAddDto  model , CancellationToken cancellationToken) 
		{
			var label = _mapper.Map<CitizenLabelDetail>(model);
			var id = await _labelDetailRepository.AddAsync(label, cancellationToken);
			if (!string.IsNullOrEmpty(id))
			{
				await LabelJoinAsync(model.CitizenId,cancellationToken);
			}
		}

		public async Task LabelDeleteAsync(CitizenLabelDetail model, CancellationToken cancellationToken)
		{
			await _labelDetailRepository.RemoveAsync(model,false,cancellationToken);
			await LabelJoinAsync(model.CitizenId, cancellationToken);
		}

		public async Task LabelJoinAsync(string citizenId, CancellationToken cancellationToken) 
		{
			var labels = await _labelDetailRepository.Queryable().Where(x => x.CitizenId == citizenId).ToListAsync(cancellationToken);
			if (labels.Any())
			{
				var labelList = labels.Select(x => x.Label).ToList();
				var label =  string.Join(",", labelList);
				var citizen = await GetAsync(citizenId,cancellationToken);
				if (citizen != null)
				{
					citizen.Label = label;
					citizen.LastModificationName = _sessionContext.UserName;
					await UpdateAsync(citizen, cancellationToken);
				}
			}
		}
	}
}
