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

namespace eSuite.API.UnitTests.Controllers.AuthenticationControllerUnitTests;

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

    [Test]
    public void CompleteEmailAction_PasswordResetAndPasswordNotSupplied_ReturnsBadRequest()
    {
        //Arrange
        var token = new EmailActionToken
        {
            Email = "forgetful.user@sun-strategy.com",
            Token = new Guid("{522E20CB-52AC-456B-BD6C-E1D65F9DAA83}")
        };

        var detailMessage = "Password missing";

        _userManagerMock.Setup(x => x.CompleteEmailAction(token, It.IsAny<CancellationToken>())).Throws(new ArgumentException(detailMessage));

        //Assert
        Assert.ThrowsAsync<ArgumentException>(async () =>
        {
            //Act
            var actualResult = await _authenticationController.CompleteEmailAction(token);
        });
    }

    [Test]
    public void CompleteEmailAction_TokenInvalid_ReturnsBadRequest()
    {
        //Arrange
        var token = new EmailActionToken
        {
            Email = "evil.hacker@sun-strategy.com",
            Token = new Guid("{522E20CB-52AC-456B-BD6C-E1D65F9DAA83}")
        };

        _userManagerMock.Setup(x => x.CompleteEmailAction(token, It.IsAny<CancellationToken>())).Throws(new TokenInvalidException());

        //Assert
        Assert.ThrowsAsync<TokenInvalidException>(async () =>
        {
            //Act
            var actualResult = await _authenticationController.CompleteEmailAction(token);
        });
    }

    [Test]
    public void CompleteEmailAction_EmailDoesNotMatchToken_ReturnsBadRequest()
    {
        //Arrange
        var token = new EmailActionToken
        {
            Email = "evil.hacker@sun-strategy.com",
            Token = new Guid("{522E20CB-52AC-456B-BD6C-E1D65F9DAA83}")
        };

        _userManagerMock.Setup(x => x.CompleteEmailAction(token, It.IsAny<CancellationToken>())).Throws(new InvalidEmailException());

        //Assert
        Assert.ThrowsAsync<InvalidEmailException>(async () =>
        {
            //Act
            var actualResult = await _authenticationController.CompleteEmailAction(token);
        });
    }

    [Test]
    public async Task CompleteEmailAction_GoodRequest_ReturnsOK()
    {
        //Arrange
        var token = new EmailActionToken
        {
            Email = "evil.hacker@sun-strategy.com",
            Token = new Guid("{522E20CB-52AC-456B-BD6C-E1D65F9DAA83}")
        };

        //Act
        var actualResult = await _authenticationController.CompleteEmailAction(token);

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

        var objectResult = actualResult as OkResult;

        Assert.That(objectResult?.StatusCode, Is.EqualTo(200));
    }

    [Test]
    public async Task CompleteEmailAction_SentinelGetsInvolved_ReturnsSentinelsResult()
    {
        //Arrange
        var token = new EmailActionToken
        {
            Email = "evil.hacker@sun-strategy.com",
            Token = new Guid("{522E20CB-52AC-456B-BD6C-E1D65F9DAA83}")
        };

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

        //Act
        var actualResult = await _authenticationController.CompleteEmailAction(token);

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