namespace Predictalytics.Application.Services;

/// <summary>
/// Pure helper: shrinks the retention window as the database approaches its size budget.
/// No DB access here — fully unit-testable.
/// </summary>
public static class StorageGovernor
{
    /// <summary>
    /// Returns the retention window (in days) to actually use.
    /// - Below 80% of the budget: the configured window (no tightening).
    /// - Between 80% and 100%: linearly interpolated down towards minRetentionDays.
    /// - At or above 100%: minRetentionDays.
    /// </summary>
    public static int ComputeEffectiveRetentionDays(
        double currentSizeGb, double maxSizeGb, int configuredRetentionDays, int minRetentionDays)
    {
        if (maxSizeGb <= 0 || configuredRetentionDays <= minRetentionDays)
            return configuredRetentionDays;

        double softStart = 0.80 * maxSizeGb;
        if (currentSizeGb <= softStart) return configuredRetentionDays;
        if (currentSizeGb >= maxSizeGb) return minRetentionDays;

        // linear interpolation between softStart (=configured) and maxSizeGb (=min)
        double t = (currentSizeGb - softStart) / (maxSizeGb - softStart); // 0..1
        double days = configuredRetentionDays - t * (configuredRetentionDays - minRetentionDays);
        return (int)System.Math.Round(days);
    }
}
