using e_suite.API.Common.models;
using eSuite.Core.Miscellaneous;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;

namespace eSuite.API.UnitTests.Controllers.SsoManagerControllerUnitTests;

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

    [Test]
    public async Task GetSsoProviderAsync_ByGuidOnly_ReturnsOk()
    {
        //Arrange
        var guid = new Guid("db016cab-ca37-4c44-bcb1-78bf4c6ec800");
        var providerName = "TestName";
        long id = 1;
        var idRef = new GeneralIdRef
        {
            Guid = guid,
            Id = null
        };

        var formObj = new ReadSsoProvider()
        {
            Name = providerName,
            Id = id,
            Guid = guid,
        };
        SsoManagerMock!
            .Setup(x => x.GetSsoProviderAsync(It.Is<GeneralIdRef>(f => f.Id == null && f.Guid == guid),
                It.IsAny<CancellationToken>())).ReturnsAsync(formObj);

        //Act
        var res = await SsoManagerController.GetProvider(idRef);

        //Assert
        var objectResult = res as OkObjectResult;
        Assert.That(objectResult?.Value, Is.Not.Null);
        if (objectResult?.Value is not null)
        {
            Assert.That(objectResult.Value.GetType(), Is.EqualTo(typeof(ReadSsoProvider)));
            var form = objectResult?.Value as ReadSsoProvider;
            Assert.That(form?.Name, Is.EqualTo(providerName));
            Assert.That(form?.Guid, Is.EqualTo(guid));
            Assert.That(form?.Id, Is.EqualTo(id));
        }
    }
}