package service

import (
	"context"

	"git.gocasts.ir/ebhomengo/niki/domain/wallet/entity"
	"git.gocasts.ir/ebhomengo/niki/domain/wallet/param"
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)

func (s Service) GetUserTransactionHistory(ctx context.Context, request param.TransactionRequest) (param.TransactionResponse, error) {
	const op = richerror.Op("wallet.service.GetUserTransactionHistory")

	transactionList, err := s.repo.GetTransactionListByUserID(ctx, request.UserID)

	if err != nil {
		return param.TransactionResponse{}, err
	}

	return param.TransactionResponse{Transaction: transactionEntityToTransactionInfo(transactionList)}, nil

}

func transactionEntityToTransactionInfo(TransactionList []entity.Transaction) []param.TransactionInfo {
	transactionInfoList := make([]param.TransactionInfo, len(TransactionList))
	for i, transaction := range TransactionList {
		transactionInfoList[i] = param.TransactionInfo{
			ID:         transaction.ID,
			UserID:     transaction.UserID,
			Amount:     transaction.Amount,
			Currency:   transaction.Currency,
			ActionType: transaction.ActionType,
			Timestamp:  transaction.Timestamp,
		}
	}

	return transactionInfoList

}
