package de.avatic.lcc.service.calculation.execution.steps;

import de.avatic.lcc.model.db.properties.SystemPropertyMappingId;
import de.avatic.lcc.repositories.properties.PropertyRepository;
import org.springframework.stereotype.Service;

@Service
public class ShippingFrequencyCalculationService {

    private final PropertyRepository propertyRepository;

    public ShippingFrequencyCalculationService(PropertyRepository propertyRepository) {
        this.propertyRepository = propertyRepository;
    }

    public double doCalculation(Integer setId, double huAnnualAmount, int maxHuPerContainer, boolean fillContainer) {
        int minAnnualFrequency = Integer.parseInt(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.FREQ_MIN, setId).orElseThrow().getCurrentValue());
        int maxAnnualFrequency = Integer.parseInt(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.FREQ_MAX, setId).orElseThrow().getCurrentValue());

        if (fillContainer) {
            var fullContainers = huAnnualAmount / maxHuPerContainer;
            return fullContainers > minAnnualFrequency ? fullContainers : minAnnualFrequency;
        }

        if (huAnnualAmount > (double) maxAnnualFrequency)
            return maxAnnualFrequency;

        return Math.max(huAnnualAmount, minAnnualFrequency);

    }

}
