package de.effigenix.domain.inventory;

import de.effigenix.shared.common.Result;

import java.math.BigDecimal;

public record TemperatureRange(BigDecimal minTemperature, BigDecimal maxTemperature) {

    private static final BigDecimal MIN_ALLOWED = new BigDecimal("-50");
    private static final BigDecimal MAX_ALLOWED = new BigDecimal("80");

    public static Result<StorageLocationError, TemperatureRange> of(
            BigDecimal minTemperature, BigDecimal maxTemperature) {
        if (minTemperature == null || maxTemperature == null) {
            return Result.failure(new StorageLocationError.InvalidTemperatureRange(
                    "Both min and max temperature must be provided"));
        }
        if (minTemperature.compareTo(maxTemperature) >= 0) {
            return Result.failure(new StorageLocationError.InvalidTemperatureRange(
                    "Min temperature must be less than max temperature"));
        }
        if (minTemperature.compareTo(MIN_ALLOWED) < 0 || minTemperature.compareTo(MAX_ALLOWED) > 0) {
            return Result.failure(new StorageLocationError.InvalidTemperatureRange(
                    "Min temperature must be between -50°C and +80°C"));
        }
        if (maxTemperature.compareTo(MIN_ALLOWED) < 0 || maxTemperature.compareTo(MAX_ALLOWED) > 0) {
            return Result.failure(new StorageLocationError.InvalidTemperatureRange(
                    "Max temperature must be between -50°C and +80°C"));
        }
        return Result.success(new TemperatureRange(minTemperature, maxTemperature));
    }

    public boolean contains(BigDecimal temperature) {
        return temperature.compareTo(minTemperature) >= 0
                && temperature.compareTo(maxTemperature) <= 0;
    }
}
