package de.szut.casino.shared.service;

import de.szut.casino.shared.dto.BetDto;
import de.szut.casino.user.UserEntity;
import de.szut.casino.user.UserRepository;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class BalanceService {
    private final UserRepository userRepository;

    public BalanceService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public boolean hasFunds(UserEntity user, BetDto betDto) {
        BigDecimal balance = user.getBalance();
        BigDecimal betAmount = betDto.getBetAmount();

        return betAmount.compareTo(balance) <= 0;
    }

    public void addFunds(UserEntity user, BigDecimal amount) {
        user.addBalance(amount);

        this.userRepository.save(user);
    }

    public void subtractFunds(UserEntity user, BigDecimal amount) {
        user.subtractBalance(amount);

        this.userRepository.save(user);
    }
}
