package bank;

/**
 * Una cantidad de dinero en una moneda: un valor inmutable.
 * El compilador genera el constructor, los métodos de acceso cents() y currency(),
 * equals, hashCode y toString.
 */
public record Money(long cents, String currency) {

    /** Constructor compacto: valida antes de asignar los campos. */
    public Money {
        if (cents < 0) {
            throw new IllegalArgumentException("monto negativo: " + cents);
        }
        if (currency == null || currency.length() != 3) {
            throw new IllegalArgumentException("código de moneda inválido: " + currency);
        }
    }

    /** Atajo para soles: Money.soles(1050) es S/ 10.50. */
    public static Money soles(long cents) {
        return new Money(cents, "PEN");
    }

    /** Devuelve una suma nueva; this no cambia. */
    public Money plus(Money other) {
        if (!currency.equals(other.currency)) {
            throw new IllegalArgumentException("monedas distintas: " + currency + " y " + other.currency);
        }
        return new Money(cents + other.cents, currency);
    }
}
