namespace WalletService.API.Application.Queries;

using MediatR;
using WalletService.Domain.AggregatesModel.WalletAggregate;

/// <summary>
/// EN: Handler for GetWalletQuery.
/// VI: Handler cho GetWalletQuery.
/// </summary>
public class GetWalletQueryHandler : IRequestHandler<GetWalletQuery, WalletDto?>
{
    private readonly IWalletRepository _walletRepository;

    public GetWalletQueryHandler(IWalletRepository walletRepository)
    {
        _walletRepository = walletRepository;
    }

    public async Task<WalletDto?> Handle(GetWalletQuery request, CancellationToken cancellationToken)
    {
        var wallet = await _walletRepository.GetByUserIdAsync(request.UserId);
        
        if (wallet == null)
            return null;

        // EN: Get balance for default currency
        // VI: Lấy số dư cho tiền tệ mặc định
        var currencyType = wallet.DefaultCurrencyType ?? CurrencyType.VND;
        var balance = wallet.GetBalance(currencyType);

        return new WalletDto(
            wallet.Id,
            wallet.UserId,
            balance,
            currencyType.Name,
            wallet.Status.Name,
            wallet.CreatedAt,
            wallet.UpdatedAt
        );
    }
}
