namespace CMSMicroservice.Application.UserWalletCQ.Queries.GetUserWallet;
public class GetUserWalletQueryHandler : IRequestHandler<GetUserWalletQuery, GetUserWalletResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<GetUserWalletResponseDto> Handle(GetUserWalletQuery request,
        CancellationToken cancellationToken)
    {
        // UserId must be provided - customer endpoints resolve from JWT before calling
        var userId = request.Id;
        if (userId == 0)
            throw new ArgumentException("UserId is required");
        
        var response = await _context.UserWallets
            .AsNoTracking()
            .Where(x => x.UserId == userId)  // Changed from x.Id to x.UserId
            .ProjectToType<GetUserWalletResponseDto>()
            .FirstOrDefaultAsync(cancellationToken);

        return response ?? throw new NotFoundException(nameof(UserWallet), userId);
    }
}
