using e_suite.Database.Core.Tables.UserManager;
using NUnit.Framework;
using UserManager.UnitTests.Helpers;

namespace UserManager.UnitTests.UserManager;

[TestFixture]
public class GetUserWithSingleUseGuidUnitTests : UserManagerTestBase<object>
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

    [Test]
    public async Task GetUserWithSingleUseGuid_GuidExists_ReturnsUserAndDeletedSingleUseGuid()
    {
        //Arrange
        var user = new User
        {
            Id = 2,
            Email = "testuser@sun-strategy.com"
        };

        await UserManagerRepository.AddUser(AuditUserDetails, user, default);

        var guid = new Guid("{91995044-F53D-4552-B154-2C33113417CD}");

        var testSingleUseGuid = new SingleUseGuid
        {
            Id = 1,
            Guid = guid,
            UserId = user.Id,
            User = user,
            Expires = _fakeClock.GetNow.AddHours(1)
        };

        UserManagerRepository.SingleUseGuids.Add(testSingleUseGuid);

        //Act
        var result = await UserManager.GetUserWithSingleUseGuid(guid, CancellationToken.None);

        //Assert
        Assert.That(result, Is.Not.Null);
        Assert.That( UserManagerRepository.SingleUseGuids.Contains(testSingleUseGuid), Is.False);
    }

    [Test]
    public async Task GetUserWithSingleUseGuid_GuidDoesNotExist_ReturnsNull()
    {
        //Arrange
        var user = new User
        {
            Id = 2,
            Email = "testuser@sun-strategy.com"
        };

        await UserManagerRepository.AddUser(AuditUserDetails, user, default);

        var guid = new Guid("{91995044-F53D-4552-B154-2C33113417CD}");

        //Act
        var result = await UserManager.GetUserWithSingleUseGuid(guid, CancellationToken.None);

        //Assert
        Assert.That(result, Is.Null);
    }
}