using e_suite.Database.Core.Tables.Domain;
using e_suite.Database.Core.Tables.UserManager;
using e_suite.Modules.DomainManager.UnitTests.Helpers;
using e_suite.Utilities.Pagination;
using NUnit.Framework;

namespace e_suite.Modules.DomainManager.UnitTests;

[TestFixture]
public class GetDomainsAsyncUnitTests : DomainManagerTestBase
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

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

        var testDomain1 = new Domain
        {
            Guid = new Guid("c7645f28-c492-4bd5-9b2e-68b9a2eb3e39"),
            Id = 1,
            Name = "Test Domain 1"
        };

        DomainManagerRepository.Domains.Add(testDomain1);

        //Act
        var result = await DomainManager.GetDomainsAsync(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));

        var firstDomain = result.Data.First();
        Assert.That(firstDomain.Guid, Is.EqualTo(testDomain1.Guid));
        Assert.That(firstDomain.Id, Is.EqualTo(testDomain1.Id));
        Assert.That(firstDomain.Name, Is.EqualTo(testDomain1.Name));
        Assert.That(firstDomain.SsoProviderId, Is.Null);
    }

    [Test]
    public async Task GetDomainsAsync_WhenDomainHasSsoProviderId_ReturnsSsoProviderId()
    {
        //Arrange
        var ssoProviderId = 1;
        var ssoProviderGuid = new Guid("49A2F070-7455-4C1C-9E57-C8E0E2527DD6");

        var paging = new Paging();

        var testDomain1 = new Domain
        {
            Guid = new Guid("c7645f28-c492-4bd5-9b2e-68b9a2eb3e39"),
            Id = 1,
            Name = "Test Domain 1",
            SsoProviderId = ssoProviderId,
            SsoProvider = new SsoProvider
            {
                Id = 1,
                Guid = ssoProviderGuid,
                Name = "Test Provider"
            }
        };

        DomainManagerRepository.Domains.Add(testDomain1);

        //Act
        var result = await DomainManager.GetDomainsAsync(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));

        var firstDomain = result.Data.First();
        Assert.That(firstDomain.SsoProviderId, Is.Not.Null);
        Assert.That(firstDomain.SsoProviderId?.Id, Is.EqualTo(ssoProviderId));
        Assert.That(firstDomain.SsoProviderId?.Guid, Is.EqualTo(ssoProviderGuid));
    }
}