package de.szut.casino.user.transaction;

import de.szut.casino.deposit.TransactionEntity;
import de.szut.casino.deposit.TransactionRepository;
import de.szut.casino.user.UserEntity;
import de.szut.casino.user.UserService;
import de.szut.casino.user.transaction.dto.GetTransactionDto;
import de.szut.casino.user.transaction.dto.UserTransactionsDto;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GetTransactionService {

    private final UserService userService;

    private final TransactionRepository transactionRepository;

    public GetTransactionService(UserService userService, TransactionRepository transactionRepository) {
        this.userService = userService;
        this.transactionRepository = transactionRepository;
    }

    public UserTransactionsDto getUserTransactionsDto(Integer limit, Integer offset) {
        UserEntity user = userService.getCurrentUser();

        List<TransactionEntity> transactionEntities = this.transactionRepository.findByUserIdWithLimit(user, limit, offset);
        Boolean hasMore = this.transactionRepository.hasMore(user, limit, offset);

        return new UserTransactionsDto(mapTransactionsToDtos(transactionEntities), hasMore);
    }

    public List<GetTransactionDto> mapTransactionsToDtos(List<TransactionEntity> transactions) {
        return transactions.stream()
                .map(transaction -> new GetTransactionDto(
                        transaction.getAmount(),
                        transaction.getStatus(),
                        transaction.getCreatedAt())
                ).toList();
    }
}

