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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class GetTransactionService {

    @Autowired
    private UserService userService;

    @Autowired
    private TransactionRepository transactionRepository;

    public UserTransactionsDto getUserTransactionsDto(String authToken, Integer limit, Integer offset) {
        Optional<UserEntity> user = this.userService.getCurrentUser();
        if (user.isPresent()) {
            List<TransactionEntity> transactionEntities = this.transactionRepository.findByUserIdWithLimit(user.get(), limit, offset);
            Boolean hasMore = this.transactionRepository.hasMore(user.get(), limit, offset);

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

        return new UserTransactionsDto(List.of(), false);
    }

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

