using e_suite.Database.Audit;
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 UnBlockIPUnitTests : BlockedIPsManagerTestBase
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

    [Test]
    public async Task UnBlockedIPAddress_FakeRepository_WhenCalled_ReturnsExpectedResult()
    {
        //Arrange
        var auditDetails = new AuditUserDetails
        {
            UserId = 1,
            UserDisplayName = "Test User",
            Comment = string.Empty
        };

        var dto = _fakeClock.GetNow.Subtract(new TimeSpan(10));

        var failedLoginAttemptOne = new FailedAccessAttempt
        {
            IPAddress = "127.0.0.1",
            AttemptedTime = dto,
        };

        var failedLoginAttemptTwo = new FailedAccessAttempt
        {
            IPAddress = "127.0.0.2",
            AttemptedTime = dto,
        };
        BlockedIPsManagerRepository.FailedAccessAttempts.Add(failedLoginAttemptOne);
        BlockedIPsManagerRepository.FailedAccessAttempts.Add(failedLoginAttemptTwo);

        //Act
        await BlockedIPsManagerRepository.UnBlockIP(auditDetails, "127.0.0.1", CancellationToken.None);
        var result = BlockedIPsManagerRepository.GetBlockedIPs(_fakeClock.GetNow).ToList();

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

    [Test]
    public async Task UnBlockedIPAddress_Manager_WhenCalled_ReturnsExpectedResult()
    {
        //Arrange
        var paging = new Paging();
        var auditDetails = new AuditUserDetails
        {
            UserId = 1,
            UserDisplayName = "Test User",
            Comment = string.Empty
        };

        var dto = new DateTimeOffset(new DateTime(2023, 09, 8, 0, 0, 0, DateTimeKind.Utc)).ToOffset(TimeSpan.FromHours(0));

        var failedLoginAttemptOne = new FailedAccessAttempt
        {
            IPAddress = "127.0.0.1",
            AttemptedTime = dto,
        };

        var failedLoginAttemptTwo = new FailedAccessAttempt
        {
            IPAddress = "127.0.0.2",
            AttemptedTime = dto,
        };
        BlockedIPsManagerRepository.FailedAccessAttempts.Add(failedLoginAttemptOne);
        BlockedIPsManagerRepository.FailedAccessAttempts.Add(failedLoginAttemptTwo);

        //Act
        await BlockedIPManager.UnblockIPAddress(auditDetails, "127.0.0.1", CancellationToken.None);
        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();
    }
}
