namespace WalletService.API.Application.Queries;

using MediatR;
using WalletService.Domain.AggregatesModel.PointAccountAggregate;

/// <summary>
/// EN: Handler for GetPointAccountQuery
/// VI: Handler cho GetPointAccountQuery
/// </summary>
public class GetPointAccountQueryHandler : IRequestHandler<GetPointAccountQuery, PointAccountDto?>
{
    private readonly IPointAccountRepository _pointAccountRepository;

    public GetPointAccountQueryHandler(IPointAccountRepository pointAccountRepository)
    {
        _pointAccountRepository = pointAccountRepository;
    }

    public async Task<PointAccountDto?> Handle(
        GetPointAccountQuery request,
        CancellationToken cancellationToken)
    {
        var account = await _pointAccountRepository.GetByUserIdAsync(request.UserId);

        if (account == null)
            return null;

        return new PointAccountDto(
            account.Id,
            account.UserId,
            account.TotalPoints,
            account.AvailablePoints,
            account.CreatedAt,
            account.UpdatedAt
        );
    }
}
