package de.effigenix.domain.masterdata.customer;

import de.effigenix.domain.masterdata.Unit;
import de.effigenix.domain.masterdata.article.ArticleId;
import de.effigenix.shared.common.Money;
import de.effigenix.shared.common.Result;

import java.math.BigDecimal;

/**
 * Value Object representing a line item in a frame contract.
 */
public record ContractLineItem(
        ArticleId articleId,
        Money agreedPrice,
        BigDecimal agreedQuantity,
        Unit unit
) {

    public ContractLineItem {
        if (articleId == null) {
            throw new IllegalArgumentException("ArticleId must not be null");
        }
        if (agreedPrice == null) {
            throw new IllegalArgumentException("Agreed price must not be null");
        }
        if (agreedQuantity != null && agreedQuantity.compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException("Agreed quantity must be positive when set");
        }
    }

    public static Result<String, ContractLineItem> create(
            ArticleId articleId, Money agreedPrice, BigDecimal agreedQuantity, Unit unit) {
        try {
            return Result.success(new ContractLineItem(articleId, agreedPrice, agreedQuantity, unit));
        } catch (IllegalArgumentException e) {
            return Result.failure(e.getMessage());
        }
    }
}
