package de.effigenix.domain.inventory;

import de.effigenix.shared.common.Result;

import java.util.Objects;

public record BatchReference(String batchId, BatchType batchType) {

    public BatchReference {
        Objects.requireNonNull(batchId, "batchId must not be null");
        Objects.requireNonNull(batchType, "batchType must not be null");
    }

    public static Result<StockError, BatchReference> of(String batchId, String batchType) {
        if (batchId == null || batchId.isBlank()) {
            return Result.failure(new StockError.InvalidBatchReference("batchId must not be blank"));
        }

        BatchType type;
        try {
            type = BatchType.valueOf(batchType);
        } catch (IllegalArgumentException | NullPointerException e) {
            return Result.failure(new StockError.InvalidBatchReference(
                    "Invalid batchType: " + batchType + ". Allowed values: PRODUCED, PURCHASED"));
        }

        return Result.success(new BatchReference(batchId, type));
    }
}
