namespace CMSMicroservice.Application.UserCQ.Queries.GetUser;
public class GetUserQueryHandler : IRequestHandler<GetUserQuery, GetUserResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<GetUserResponseDto> Handle(GetUserQuery 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.Users
            .AsNoTracking()
            .Where(x => x.Id == userId)
            .ProjectToType<GetUserResponseDto>()
            .FirstOrDefaultAsync(cancellationToken);

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