package de.szut.casino.user;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.math.BigDecimal;

@Setter
@Getter
@Entity
@NoArgsConstructor
public class UserEntity {
    @Id
    @GeneratedValue
    private Long id;
    
    @Column(unique = true)
    private String email;
    
    @Column(unique = true)
    private String username;
    
    private String password;
    
    @Column(precision = 19, scale = 2)
    private BigDecimal balance;

    public UserEntity(String email, String username, String password, BigDecimal balance) {
        this.email = email;
        this.username = username;
        this.password = password;
        this.balance = balance;
    }

    public void addBalance(BigDecimal amountToAdd) {
        if (amountToAdd == null || amountToAdd.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }

        if (this.balance == null) {
            this.balance = BigDecimal.ZERO;
        }

        this.balance = this.balance.add(amountToAdd);
    }

    public void subtractBalance(BigDecimal amountToSubtract) {
        if (amountToSubtract == null || amountToSubtract.compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException("Amount to subtract must be positive.");
        }

        if (this.balance == null) {
            this.balance = BigDecimal.ZERO;
        }

        if (this.balance.compareTo(amountToSubtract) < 0) {
            throw new IllegalStateException("Insufficient funds to subtract " + amountToSubtract);
        }

        this.balance = this.balance.subtract(amountToSubtract);
    }
}
