using Brizco.Domain.Entities.Complex;
using Microsoft.IdentityModel.Tokens;

namespace Brizco.Repository.Handlers.Complex;

public class GetComplexUsersQueryHandler : IRequestHandler<GetComplexUsersQuery, List<ComplexUserSDto>>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetComplexUsersQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<List<ComplexUserSDto>> Handle(GetComplexUsersQuery request, CancellationToken cancellationToken)
    {
        if (!request.ComplexId.IsNullOrEmpty() && Guid.TryParse(request.ComplexId, out Guid complexId))
            return await _repositoryWrapper.SetRepository<ComplexUser>().TableNoTracking
                .Where(c => c.ComplexId == complexId)
                .OrderByDescending(s => s.CreatedAt)
                .Skip(request.Page * 15).Take(15)
                .Select(ComplexUserMapper.ProjectToSDto)
                .ToListAsync(cancellationToken);


        return await _repositoryWrapper.SetRepository<ComplexUser>().TableNoTracking
            .OrderByDescending(s => s.CreatedAt)
            .Skip(request.Page * 15).Take(15)
            .Select(ComplexUserMapper.ProjectToSDto)
            .ToListAsync(cancellationToken);

    }
}