using e_suite.API.Common;
using e_suite.Service.Performance.Repository;
using eSuite.Core.Clock;
using Microsoft.Extensions.Configuration;

namespace e_suite.Service.Performance;

public class PerformanceMaintenance : IPerformanceMaintenance
{
    private readonly IClock _clock;
    private readonly IConfiguration _configuration;
    private readonly IPerformanceRepository _performanceRepository;

    public PerformanceMaintenance(IPerformanceRepository performanceRepository, IConfiguration configuration, IClock clock)
    {
        _performanceRepository = performanceRepository;
        _configuration = configuration;
        _clock = clock;
    }

    protected int GetLoginAttemptConstraints()
    {
        return _configuration.GetValue<int>("Performance:DaysToKeep");
    }

    public async Task ClearOldPerformanceData()
    {
        var daysToKeep = GetLoginAttemptConstraints();

        var cutoffTime = _clock.GetNow.AddDays(-daysToKeep);
        await _performanceRepository.DeleteLogsBefore(cutoffTime);
    }
}