using e_suite.API.Common.exceptions;
using e_suite.Database.Core.Tables.Domain;
using e_suite.Database.Core.Tables.UserManager;
using Moq;
using NUnit.Framework;
using UserManager.UnitTests.Helpers;

namespace UserManager.UnitTests.UserManager;

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

    [Test]
    public Task GetProfile_UserNotExists_ThrowsError()
    {
        //Arrange
        const string email = "non.existant@sun-strategy.com";

        //Act & Assert
        Assert.ThrowsAsync<NotFoundException>(async () => { await UserManager.GetProfile(email); });
        return Task.CompletedTask;
    }

    [Test]
    public async Task GetProfile_UserExists_ReturnsProfile()
    {
        //Arrange
        var existingUser = new User
        {
            Email = "testuser@sun-strategy.com",
            Domain = new Domain
            {
                Name = "TestDomain"
            }
        };
        await UserManagerRepository.AddUser(AuditUserDetails, existingUser, default);

        //Act
        var profile = await UserManager.GetProfile(existingUser.Email);

        //Assert
        Assert.That( existingUser.Email, Is.EqualTo(profile.Email));
    }

    [Test]
    public async Task GetProfile_UsesTwoFactorAuthentication_DoesNotGenerateNewCode()
    {
        //Arrange
        var existingUser = new User
        {
            Email = "testuser@sun-strategy.com",
            Domain = new Domain
            {
                Name = "TestDomain"
            },
            UsingTwoFactorAuthentication = true
        };
        await UserManagerRepository.AddUser(AuditUserDetails, existingUser, default);

        //Act
        var profile = await UserManager.GetProfile(existingUser.Email);

        //Assert
        RandomNumberGeneratorMock.Verify(x => x.GetBytes(It.IsAny<byte[]>()), Times.Never);
    }
}