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

namespace eSuite.API.UnitTests.Controllers.AuthenticationControllerUnitTests;

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

    [TestCase("test@test.com")]
    [TestCase("hacker@evilwebsite.org")]
    [Obsolete("_authenticationController.Login is obsolete")]
    public async Task Login_EmailNotFound_Unauthorised(string emailAddress)
    {
        //Arrange
        var login = new Login
        {
            Email = emailAddress,
            Password = string.Empty
        };

        var loginResponse = new LoginResponse
        {
            Result = LoginResult.Failed
        };

        _userManagerMock.Setup(x => x.Login(login, It.IsAny<CancellationToken>())).Returns(Task.FromResult(loginResponse));

        //Act
        var actualResult = await _authenticationController.Login(login);

        //Assert
        Assert.That(actualResult.GetType(), Is.EqualTo(typeof(UnauthorizedObjectResult)));

        var objectResult = actualResult as UnauthorizedObjectResult;

        Assert.That(objectResult?.StatusCode, Is.EqualTo(401));
        Assert.That(objectResult?.Value, Is.Not.Null);

        if (objectResult?.Value != null)
        {
            Assert.That(objectResult.Value.GetType, Is.EqualTo(typeof(ProblemDetails)));

            var problemDetails = objectResult.Value as ProblemDetails;

            Assert.That(problemDetails?.Title, Is.EqualTo(AccessDeniedText));
        }
    }

    [TestCase("testuser1@sun-strategy.com")]
    [Obsolete("_authenticationController.Login is obsolete")]
    public async Task Login_Success_OKAndToken(string emailAddress)
    {
        //Arrange
        var login = new Login
        {
            Email = emailAddress,
            Password = string.Empty
        };

        var loginResponse = new LoginResponse
        {
            Result = LoginResult.Success,
            Token = "This is a test token"
        };

        _userManagerMock.Setup(x => x.Login(login, It.IsAny<CancellationToken>())).Returns(Task.FromResult(loginResponse));

        //Act
        var actualResult = await _authenticationController.Login(login);

        //Assert
        Assert.That(actualResult.GetType(), Is.EqualTo(typeof(OkObjectResult)));

        var objectResult = actualResult as OkObjectResult;

        Assert.That(objectResult?.StatusCode, Is.EqualTo(200));
        Assert.That(objectResult?.Value, Is.Not.Null);

        if (objectResult?.Value != null)
        {
            Assert.That(objectResult.Value.GetType, Is.EqualTo(typeof(SuccessfulLogin)));

            var problemDetails = objectResult.Value as SuccessfulLogin;

            Assert.That(problemDetails?.Title, Is.EqualTo(LoginAcceptedText));
            Assert.That(problemDetails?.Token, Is.EqualTo(loginResponse.Token));
        }
    }

    [TestCase("testuser1@sun-strategy.com")]
    [Obsolete("_authenticationController.Login is obsolete")]
    public async Task Login_TwoFactorAuthenticationCodeIncorrect_Unauthorised(string emailAddress)
    {
        //Arrange
        var login = new Login
        {
            Email = emailAddress,
            Password = string.Empty,
            SecurityCode = "12345"
        };

        var loginResponse = new LoginResponse
        {
            Result = LoginResult.TwoFactorAuthenticationCodeIncorrect
        };

        _userManagerMock.Setup(x => x.Login(login, It.IsAny<CancellationToken>())).Returns(Task.FromResult(loginResponse));

        //Act
        var actualResult = await _authenticationController.Login(login);

        //Assert
        Assert.That(actualResult.GetType(), Is.EqualTo(typeof(UnauthorizedObjectResult)));

        var objectResult = actualResult as UnauthorizedObjectResult;

        Assert.That(objectResult?.StatusCode, Is.EqualTo(401));
        Assert.That(objectResult?.Value, Is.Not.Null);

        if (objectResult?.Value != null)
        {
            Assert.That(objectResult.Value.GetType, Is.EqualTo(typeof(ProblemDetails)));

            var problemDetails = objectResult.Value as ProblemDetails;

            Assert.That(problemDetails?.Title, Is.EqualTo(AccessDeniedText));
        }
    }

    [TestCase("testuser1@sun-strategy.com")]
    [Obsolete("_authenticationController.Login is obsolete")]
    public async Task Login_TwoFactorAuthenticationCodeRequired_AcceptedWithDetails(string emailAddress)
    {
        //Arrange
        var login = new Login
        {
            Email = emailAddress,
            Password = string.Empty
        };

        var loginResponse = new LoginResponse
        {
            Result = LoginResult.TwoFactorAuthenticationCodeRequired
        };

        _userManagerMock.Setup(x => x.Login(login, It.IsAny<CancellationToken>())).Returns(Task.FromResult(loginResponse));

        //Act
        var actualResult = await _authenticationController.Login(login);

        //Assert
        Assert.That(actualResult.GetType(), Is.EqualTo(typeof(AcceptedResult)));

        var objectResult = actualResult as AcceptedResult;

        Assert.That(objectResult?.StatusCode, Is.EqualTo(202));
        Assert.That(objectResult?.Value, Is.Not.Null);

        if (objectResult?.Value != null)
        {
            Assert.That(objectResult.Value.GetType, Is.EqualTo(typeof(ProblemDetails)));

            var problemDetails = objectResult.Value as ProblemDetails;

            Assert.That(problemDetails?.Title, Is.EqualTo(LoginAcceptedText));
            Assert.That(problemDetails?.Detail, Is.EqualTo("Please supply Two Factor Authentication code"));
        }
    }

    [TestCase("testuser1@sun-strategy.com")]
    [Obsolete("_authenticationController.Login is obsolete")]
    public async Task Login_TwoFactorAuthenticationRemovalRequested_AcceptedWithDetails(string emailAddress)
    {
        //Arrange
        var login = new Login
        {
            Email = emailAddress,
            Password = string.Empty,
            RequestTfaRemoval = true
        };

        var loginResponse = new LoginResponse
        {
            Result = LoginResult.TwoFactorAuthenticationRemovalRequested
        };

        _userManagerMock.Setup(x => x.Login(login, It.IsAny<CancellationToken>())).Returns(Task.FromResult(loginResponse));

        //Act
        var actualResult = await _authenticationController.Login(login);

        //Assert
        Assert.That(actualResult.GetType(), Is.EqualTo(typeof(AcceptedResult)));

        var objectResult = actualResult as AcceptedResult;

        Assert.That(objectResult?.StatusCode, Is.EqualTo(202));
        Assert.That(objectResult?.Value, Is.Not.Null);

        if (objectResult?.Value != null)
        {
            Assert.That(objectResult.Value.GetType, Is.EqualTo(typeof(ProblemDetails)));

            var problemDetails = objectResult.Value as ProblemDetails;

            Assert.That(problemDetails?.Title, Is.EqualTo(LoginAcceptedText));
            Assert.That(problemDetails?.Detail, Is.EqualTo("Request to remove two factor authentication accepted"));
        }
    }

    [TestCase("testuser1@sun-strategy.com")]
    [Obsolete("_authenticationController.Login is obsolete")]
    public async Task Login_EmailNotConfirmed_AcceptedWithDetails(string emailAddress)
    {
        //Arrange
        var login = new Login
        {
            Email = emailAddress,
            Password = string.Empty,
            SecurityCode = "12345"
        };

        var loginResponse = new LoginResponse
        {
            Result = LoginResult.EmailNotConfirmed
        };

        _userManagerMock.Setup(x => x.Login(login, It.IsAny<CancellationToken>())).Returns(Task.FromResult(loginResponse));

        //Act
        var actualResult = await _authenticationController.Login(login);

        //Assert
        Assert.That(actualResult.GetType(), Is.EqualTo(typeof(UnauthorizedObjectResult)));

        var objectResult = actualResult as UnauthorizedObjectResult;

        Assert.That(objectResult?.StatusCode, Is.EqualTo(401));
        Assert.That(objectResult?.Value, Is.Not.Null);

        if (objectResult?.Value != null)
        {
            Assert.That(objectResult.Value.GetType, Is.EqualTo(typeof(ProblemDetails)));

            var problemDetails = objectResult.Value as ProblemDetails;

            Assert.That(problemDetails?.Title, Is.EqualTo("Access denied"));
            Assert.That(problemDetails?.Detail, Is.EqualTo("Email not confirmed"));
        }
    }

    [Test]
    [Obsolete("_authenticationController.Login is obsolete")]
    public async Task Login_SentinelGetsInvolved_ReturnsSentinelsResult()
    {
        //Arrange
        var login = new Login
        {
            Email = "test@test.test"
        };

        _sentinelMock.Setup(x => x.CheckSecurity(_authenticationController, It.IsAny<CancellationToken>())).Returns<ControllerBase, CancellationToken>((controller, cancellationToken) => Task.FromResult<IActionResult?>(controller.Unauthorized()));

        //Act
        var actualResult = await _authenticationController.Login(login);

        //Assert
        Assert.That(actualResult.GetType(), Is.EqualTo(typeof(UnauthorizedResult)));
    }
}