using e_suite.Database.Core.Tables.Sentinel;
using e_suite.Modules.BlockedIPsManager.UnitTests.Helpers;
using e_suite.Utilities.Pagination;
using NUnit.Framework;

namespace e_suite.Modules.BlockedIPsManager.UnitTests;

public class GetBlockedIPsUnitTests: BlockedIPsManagerTestBase
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

    [Test]
    public void GetBlockedIPs_FakeRepository_WhenCalled_ReturnsExpectedResult()
    {
        //Arrange
        var failedLoginAttemptOne = new FailedAccessAttempt
        {
            IPAddress = "127.0.0.1",
            AttemptedTime = _fakeClock.GetNow.Subtract(TimeSpan.FromSeconds(10)),
        };

        var failedLoginAttemptTwo = new FailedAccessAttempt
        {
            IPAddress = "127.0.0.1",
            AttemptedTime = _fakeClock.GetNow.Subtract(TimeSpan.FromSeconds(9)),
        };
        BlockedIPsManagerRepository.FailedAccessAttempts.Add(failedLoginAttemptOne);
        BlockedIPsManagerRepository.FailedAccessAttempts.Add(failedLoginAttemptTwo);

        //Act
        var result = BlockedIPsManagerRepository.GetBlockedIPs(_fakeClock.GetNow).ToList();

        //Assert
        Assert.That(result, Is.Not.Null);
        Assert.That(result.Count, Is.EqualTo(2));
        Assert.Pass();
    }

    [Test]
    public async Task GetBlockedIPs_Manager_WhenCalled_ReturnsExpectedResult()
    {
        //Arrange
        var paging = new Paging();

        var failedLoginAttemptOne = new FailedAccessAttempt
        {
            IPAddress = "127.0.0.1",
            AttemptedTime = _fakeClock.GetNow.Subtract(TimeSpan.FromSeconds(10)),
        };

        var failedLoginAttemptTwo = new FailedAccessAttempt
        {
            IPAddress = "127.0.0.1",
            AttemptedTime = _fakeClock.GetNow.Subtract(TimeSpan.FromSeconds(9)),
        };
        BlockedIPsManagerRepository.FailedAccessAttempts.Add(failedLoginAttemptOne);
        BlockedIPsManagerRepository.FailedAccessAttempts.Add(failedLoginAttemptTwo);

        //Act
        var result = await BlockedIPManager.GetBlockedIPs(paging, CancellationToken.None);

        //Assert
        Assert.That(result, Is.Not.Null);
        Assert.That(result.Count, Is.EqualTo(1));
        Assert.That(result.Data.Count(), Is.EqualTo(1));
        Assert.Pass();
    }
}