using e_suite.API.Common.exceptions;
using e_suite.API.Common.models;
using e_suite.Database.Core.Tables.UserManager;
using eSuite.Core.Miscellaneous;
using NUnit.Framework;
using SSOManager.UnitTests.Helpers;

namespace SSOManager.UnitTests.SsoManager;

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

    [Test]
    public async Task EditSsoProviderAsyncActiveSsoProvider_WhenChangingValues_ValuesSavedCorrectly()
    {
        //Arrange
        var ssoProvider = new SsoProvider
        {
            Id = 2,
            Guid = new Guid("48154fbc-33ce-428e-956c-87ebcc7adc83"),
            Name = "SsoProvider to Delete",
            IsPublic = true,
            Deleted = false,
            ClientId = "ClientId",
            ClientSecret = "ClientSecret",
            AuthorizationEndpoint = "AuthEndPoint",
            TokenEndpoint = "TokenEndpoint",
            ValidIssuer = "SsoProviderToDelete"
        };

        SsoManagerRepository.SsoProviders.Add(ssoProvider);

        var editSsoProvider = new EditSsoProvider()
        {
            Id = new GeneralIdRef()
            {
                
                Id = ssoProvider.Id,
                Guid = ssoProvider.Guid,
            } ,
            Name = "SsoProvider updated",
            IsPublic = true,
            ClientId = "UpdatedClientId",
            ClientSecret = "UpdatedClientSecret",
            AuthorizationEndpoint = "UpdatedAuthEndPoint",
            TokenEndpoint = "UpdatedTokenEndpoint",
            ValidIssuer = "UpdatedSsoProviderToDelete"
        };

        //Act
        await SsoManager.EditSsoProviderAsync(AuditUserDetails, editSsoProvider,
            CancellationToken.None);

        //Assert
        Assert.That(ssoProvider.Deleted, Is.False);
        Assert.That(ssoProvider.Name, Is.EqualTo(editSsoProvider.Name));
        Assert.That(ssoProvider.IsPublic, Is.EqualTo(editSsoProvider.IsPublic));
        Assert.That(ssoProvider.ClientId, Is.EqualTo(editSsoProvider.ClientId));
        Assert.That(ssoProvider.ClientSecret, Is.EqualTo(editSsoProvider.ClientSecret));
        Assert.That(ssoProvider.AuthorizationEndpoint, Is.EqualTo(editSsoProvider.AuthorizationEndpoint));
        Assert.That(ssoProvider.TokenEndpoint, Is.EqualTo(editSsoProvider.TokenEndpoint));
        Assert.That(ssoProvider.ValidIssuer, Is.EqualTo(editSsoProvider.ValidIssuer));
    }

    [Test]
    public async Task EditSsoProviderAsyncActiveSsoProvider_WhenSettingIsPublicFalse_ValuesSavedCorrectly()
    {
        //Arrange
        var ssoProvider = new SsoProvider
        {
            Id = 2,
            Guid = new Guid("48154fbc-33ce-428e-956c-87ebcc7adc83"),
            Name = "SsoProvider to Delete",
            IsPublic = true,
            Deleted = false,
            ClientId = "ClientId",
            ClientSecret = "ClientSecret",
            AuthorizationEndpoint = "AuthEndPoint",
            TokenEndpoint = "TokenEndpoint",
            ValidIssuer = "SsoProviderToDelete"
        };

        SsoManagerRepository.SsoProviders.Add(ssoProvider);

        var editSsoProvider = new EditSsoProvider()
        {
            Id = new GeneralIdRef()
            {

                Id = ssoProvider.Id,
                Guid = ssoProvider.Guid,
            },
            Name = "SsoProvider updated",
            IsPublic = false,
            ClientId = "UpdatedClientId",
            ClientSecret = "UpdatedClientSecret",
            AuthorizationEndpoint = "UpdatedAuthEndPoint",
            TokenEndpoint = "UpdatedTokenEndpoint",
            ValidIssuer = "UpdatedSsoProviderToDelete"
        };

        //Act
        await SsoManager.EditSsoProviderAsync(AuditUserDetails, editSsoProvider,
            CancellationToken.None);

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

    [Test]
    public void EditSsoProviderAsyncActiveSsoProvider_WhenIdNotFound_ThrowsNotFoundException()
    {
        //Arrange
        var ssoProvider = new SsoProvider
        {
            Id = 2,
            Guid = new Guid("48154fbc-33ce-428e-956c-87ebcc7adc83"),
            Name = "SsoProvider to Delete",
            IsPublic = true,
            Deleted = false,
            ClientId = "ClientId",
            ClientSecret = "ClientSecret",
            AuthorizationEndpoint = "AuthEndPoint",
            TokenEndpoint = "TokenEndpoint",
            ValidIssuer = "SsoProviderToDelete"
        };

        SsoManagerRepository.SsoProviders.Add(ssoProvider);

        var editSsoProvider = new EditSsoProvider
        {
            Id = new GeneralIdRef
            {
                Id = 4,
                Guid = new Guid("b537fc14-5e73-40bf-a998-07d21b65cdf9"),
            },
            Name = "SsoProvider updated",
            IsPublic = true,
            ClientId = "UpdatedClientId",
            ClientSecret = "UpdatedClientSecret",
            AuthorizationEndpoint = "UpdatedAuthEndPoint",
            TokenEndpoint = "UpdatedTokenEndpoint",
            ValidIssuer = "UpdatedSsoProviderToDelete"
        };

        //Assert
        var result = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            await SsoManager.EditSsoProviderAsync(AuditUserDetails, editSsoProvider,
                CancellationToken.None);
        });

        Assert.That(result!.Message, Is.EqualTo("SsoProvider with this id does not exists"));
    }
}