package de.effigenix.domain.masterdata.supplier;

import de.effigenix.shared.common.Result;

/**
 * Value Object representing a supplier rating with three scores (1-5).
 */
public record SupplierRating(int qualityScore, int deliveryScore, int priceScore) {

    public SupplierRating {
        validateScore("qualityScore", qualityScore);
        validateScore("deliveryScore", deliveryScore);
        validateScore("priceScore", priceScore);
    }

    public static Result<String, SupplierRating> create(int qualityScore, int deliveryScore, int priceScore) {
        try {
            return Result.success(new SupplierRating(qualityScore, deliveryScore, priceScore));
        } catch (IllegalArgumentException e) {
            return Result.failure(e.getMessage());
        }
    }

    public double averageScore() {
        return (qualityScore + deliveryScore + priceScore) / 3.0;
    }

    private static void validateScore(String name, int score) {
        if (score < 1 || score > 5) {
            throw new IllegalArgumentException(name + " must be between 1 and 5, was: " + score);
        }
    }
}
