using MediatR;
using SocialService.Domain.AggregatesModel.RelationshipAggregate;
using SocialService.Domain.AggregatesModel.UserBlockAggregate;

namespace SocialService.API.Application.Queries;

/// <summary>
/// EN: Handler for GetSocialStatisticsQuery.
/// VI: Handler cho GetSocialStatisticsQuery.
/// </summary>
public class GetSocialStatisticsQueryHandler : IRequestHandler<GetSocialStatisticsQuery, SocialStatisticsDto>
{
    private readonly IRelationshipRepository _relationshipRepository;
    private readonly IUserBlockRepository _userBlockRepository;

    public GetSocialStatisticsQueryHandler(
        IRelationshipRepository relationshipRepository,
        IUserBlockRepository userBlockRepository)
    {
        _relationshipRepository = relationshipRepository;
        _userBlockRepository = userBlockRepository;
    }

    public async Task<SocialStatisticsDto> Handle(GetSocialStatisticsQuery request, CancellationToken cancellationToken)
    {
        // EN: Get statistics from repositories
        // VI: Lấy thống kê từ repositories
        var totalFriendships = await _relationshipRepository.GetTotalCountAsync(
            RelationshipStatus.Accepted.Id, 
            RelationshipType.Friendship.Id);
        
        var totalFollowings = await _relationshipRepository.GetTotalCountAsync(
            RelationshipStatus.Accepted.Id, 
            RelationshipType.Following.Id);
        
        var pendingRequests = await _relationshipRepository.GetTotalCountAsync(
            RelationshipStatus.Pending.Id, 
            null);
        
        var totalRelationships = await _relationshipRepository.GetTotalCountAsync(null, null);
        var totalBlocks = await _userBlockRepository.GetTotalCountAsync();

        return new SocialStatisticsDto(
            TotalFriendships: totalFriendships,
            TotalFollowings: totalFollowings,
            TotalBlocks: totalBlocks,
            PendingFriendRequests: pendingRequests,
            AcceptedFriendships: totalFriendships,
            TotalRelationships: totalRelationships);
    }
}
