using e_suite.API.Common.repository;
using e_suite.Database.Core.Tables.Sentinel;
using e_suite.UnitTestCore;

namespace Sentinel.UnitTests.Repository
{
    public class FakeSentinelRepository : FakeRepository, ISentinelRepository
    {
        public int AccessAttempts = 0;
        public string IpAddress = string.Empty;
        public DateTimeOffset EarliestAttemptTime = DateTimeOffset.MinValue;
        public List<FailedAccessAttempt> FailedAccessAttempts = new();

        public Task AddFailedAccessAttempt(FailedAccessAttempt failedLoginAttempt, CancellationToken cancellationToken)
        {
            FailedAccessAttempts.Add(failedLoginAttempt);

            return Task.CompletedTask;
        }

        public Task<int> GetAccessAttemptsSince(string ipAddress, DateTimeOffset earliestAttemptTime, CancellationToken cancellationToken)
        {
            IpAddress = ipAddress;
            EarliestAttemptTime = earliestAttemptTime;

            return Task.FromResult(AccessAttempts);
        }

        public Task DeleteAccessAttemptsBefore(DateTimeOffset earliestAttemptTime)
        {
            var itemsToRemove = FailedAccessAttempts.Where(x => x.AttemptedTime < earliestAttemptTime).ToList();
            foreach (var item in itemsToRemove)
            {
                FailedAccessAttempts.Remove(item);
            }
            return Task.CompletedTask;
        }
    }
}
