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

namespace UserManager.UnitTests.UserManager;

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

    [Test]
    public async Task GetProfile_UserExists_ReturnsSsoProvider()
    {
        //Arrange
        var actualSsoProvider = UserManagerRepository.SsoProviders.First();

        var existingUser = new User
        {
            Email = "testuser@sun-strategy.com",
            SsoProviderId = actualSsoProvider.Id,
            SsoProvider = actualSsoProvider
        };

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

        //Act
        var ssoProvider = await UserManager.GetSsoProviderForEmail(existingUser.Email, CancellationToken.None);

        //Assert
        Assert.That(ssoProvider, Is.EqualTo(actualSsoProvider));
    }

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

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

        //Act
        var ssoProvider = await UserManager.GetSsoProviderForEmail(existingUser.Email, CancellationToken.None);

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

    [Test]
    public async Task GetProfile_UserHasdomainLevelSsoProvider_ReturnsSsoProvider()
    {
        //Arrange
        var actualSsoProvider = UserManagerRepository.SsoProviders.First();

        var domain = new Domain
        {
            Id = 100,
            SsoProvider = actualSsoProvider,
            SsoProviderId = actualSsoProvider.Id
        };

        DomainRepository.Domains.Add(domain);

        var existingUser = new User
        {
            Email = "testuser@sun-strategy.com",
            DomainId = domain.Id
        };

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

        //Act
        var ssoProvider = await UserManager.GetSsoProviderForEmail(existingUser.Email, CancellationToken.None);

        //Assert
        Assert.That(ssoProvider, Is.EqualTo(actualSsoProvider));
    }
}