namespace CMSMicroservice.Application.ClubMembershipCQ.Queries.GetClubMembership;

public class GetClubMembershipQueryHandler : IRequestHandler<GetClubMembershipQuery, ClubMembershipDto?>
{
    private readonly IApplicationDbContext _context;

    public GetClubMembershipQueryHandler(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<ClubMembershipDto?> Handle(GetClubMembershipQuery request, CancellationToken cancellationToken)
    {
        var membership = await _context.ClubMemberships
            .AsNoTracking()
            .Where(x => x.UserId == request.UserId)
            .Select(x => new ClubMembershipDto
            {
                Id = x.Id,
                UserId = x.UserId,
                IsActive = x.IsActive,
                ActivatedAt = x.ActivatedAt,
                InitialContribution = x.InitialContribution,
                TotalEarned = x.TotalEarned,
                Created = x.Created,
                LastModified = x.LastModified
            })
            .FirstOrDefaultAsync(cancellationToken);

        return membership;
    }
}
