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

namespace SocialService.API.Application.Queries;

/// <summary>
/// EN: Handler for GetAllBlocksQuery.
/// VI: Handler cho GetAllBlocksQuery.
/// </summary>
public class GetAllBlocksQueryHandler : IRequestHandler<GetAllBlocksQuery, GetAllBlocksResult>
{
    private readonly IUserBlockRepository _userBlockRepository;

    public GetAllBlocksQueryHandler(IUserBlockRepository userBlockRepository)
    {
        _userBlockRepository = userBlockRepository;
    }

    public async Task<GetAllBlocksResult> Handle(GetAllBlocksQuery request, CancellationToken cancellationToken)
    {
        var blocks = await _userBlockRepository.GetAllAsync(request.Skip, request.Take);
        var totalCount = await _userBlockRepository.GetTotalCountAsync();

        var dtos = blocks.Select(b => new BlockAdminDto(
            b.Id,
            b.BlockerId,
            b.BlockedId,
            b.Reason,
            b.CreatedAt));

        return new GetAllBlocksResult(dtos, totalCount);
    }
}
